Global search and replace in vi
Hello,
Does anyone know how I can perform a search and replace for the following in vi:- I have 01483012302080818220208 on lines 3, 1031 & 2065 of my file and I would like to change the first 5 characters from 01483 to 01036 on those lines. cheers... |
open the file in VI editor and type
:g/01483/s//01036 This substitutes first occurrence of 01483 on every line of the file with 01036 rgds bil |
Hi,
If the string is unique (only present at the beginning of the line and only on lines 3,1013 and 2065), you can do something like this: sed 's/^01483\(.*\)/01036\1/' infile Hope this helps. |
Does this work with HPUX
Hello,
Would this work with HPUX to ? I tried what you suggested below on my file 0036 sed -e 's/^01483\(.*\)/01036\1/' 0036 or is the above wrong ? Also - how do I specify the lines ? Sorry for all the questions |
Hi,
Quote:
Quote:
the -e option isn't needed, but the command looks correct (scan all lines of the file, if a line starts with 01483 replace it with 01036 and do this for the file called 0036). Quote:
sed '4s/X/Y/g' infile => changes all X's to Y's on line 4 sed '1000,2000s/A/B' infile => Change the first A in a line to B and do this for lines 1000 to 2000 (both are included) Hope this clears things up. |
Worked once..
Well I managed to get it to work once but I was logged in as root and couldn't recall the command and it has not worked since. The sed commands seems to run through the file and when you check the lines starting with 01483 then you find none of them have changed.
is there any way I could run the :g/01483/s//01036 command from the shell as that works fine. What I am after is shell script that changes lines 01483 and lines 17483 to 01XXX and 17XXX (where the xxx is my required number, this is also the file name to). |
Hi,
The sed command we have been talking about will not change the infile, all it will do is print the, changed, content to screen. If you have sed version 4.x you can use the -i flag to change 'in place' (do make sure the command does what you want it to do first). If you use a sed version that is older then 4.x you need 2 steps: sed '17483s/^01483\(.*\)/01036\1/' 0036 > /tmp/0036.tmp mv /tmp/0036.tmp 0036 The first step will put the output of the sed command into a new file (/tmp/0036.tmp) and the second step moves that file to 0036 (overwriting the original!!). |
All times are GMT -5. The time now is 05:29 PM. |