LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Global search and repalce in vi (https://www.linuxquestions.org/questions/linux-newbie-8/global-search-and-repalce-in-vi-626102/)

sph90457 03-06-2008 04:52 AM

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...

niceguy_81333 03-06-2008 05:13 AM

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

druuna 03-06-2008 05:14 AM

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.

sph90457 03-06-2008 06:54 AM

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

druuna 03-06-2008 08:46 AM

Hi,

Quote:

Would this work with HPUX to ?
On most (all?) HP boxes/OS's I've worked with sed is available/installed. It could be that you use an older version, but the basic functionality (which is used in the above example) is the same. So, yes it should work on HPUX.

Quote:

I tried what you suggested below on my file 0036

sed -e 's/^01483\(.*\)/01036\1/' 0036

or is the above wrong ?
Did you get any error messages?

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:

Also - how do I specify the lines ?
Sed can work with ranges (or a single line):

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.

sph90457 03-06-2008 10:59 AM

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).

druuna 03-06-2008 11:31 AM

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.