1

i want to edit file at remote server using solaris

the original file at the remote server that i want to edit is :

11111
22222
33333
44444
55555
66666
77777

and i want to remove the 5th line "55555" and replace it by "00000"

i try this

ssh user@host 'cat ~/path_of_original_file.txt '| sed 's/55555/00000/g' ;

the result appears successfully and the line replaced as i want , but when i open the file at the remote server nothing change !!!!!

mondo32
  • 33
  • 7
  • 2
    You are changing the stdout, not the file itself. To replace in place you need to do `sed -i 's/55555/00000/g' file` – fedorqui Mar 04 '14 at 09:38
  • thank you for replay ,,,,,,, i try but it respond with "sed: illegal option -- i" – mondo32 Mar 04 '14 at 09:51
  • 1
    Ok then it means that `-i` is not possible. You can then store the output in a temp file and then move to the original: `sed 's/55555/00000/g' file > temp_file && mv temp_file file` – fedorqui Mar 04 '14 at 09:52

1 Answers1

1

There are two things wrong with your attempt:

  1. You pipe the cated output to sed, so you are only changing stdout.

  2. The right-hand-side of the pipe is run locally, not on the remote server since it is outside of your quoted string.

What you probably want is

ssh user@host 'sed -i "s/55555/00000/g" ~/path_of_original_file.txt'

where -i means in-place (see man sed).

Also note that /g will change all occurrances of 55555, not just the first/the one on line 5.

Since you are on Solaris and your sed probably doesn't have -i you need to use a temporary file (see also e.g. here).

Community
  • 1
  • 1
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
  • thank you very much ,,,,, ^_^ i am new in unix so i still make lots of mistakes ,, i will try and tell you what happen – mondo32 Mar 04 '14 at 10:00