Operating System - HP-UX
1829438 Members
1124 Online
109992 Solutions
New Discussion

Re: VI string manipulation

 
SOLVED
Go to solution
James Barry Donovan
Occasional Contributor

VI string manipulation

Traditionally my background has been OpenVMS and our company recently purchased a HP UX
solution. We like some information re: the
ability on how to take a name in a flat file and using string maipulation change that name to another, for example...

for every occurrance of BOB found change it to FRANK. We like this to be a script that can be
launched via Crontab.

-barry donovan
7 REPLIES 7
Jeff Machols
Esteemed Contributor

Re: VI string manipulation

cat file | sed -e "s/BOB/FRANK/g"
Stefan Farrelly
Honored Contributor

Re: VI string manipulation


You need to use the sed command to it;

eg. if you have a file called tt.txt then;

cat tt.txt | sed 's/BOB/FRED/g' >
Im from Palmerston North, New Zealand, but somehow ended up in London...
Jeff Machols
Esteemed Contributor

Re: VI string manipulation

actually you don't even need the cat

sed -e "s/BOB/FRANK/g" file > outfile

James R. Ferguson
Acclaimed Contributor
Solution

Re: VI string manipulation

Hi:

# sed -e 's/BOB/FRANK/g' myfile > myfile.fixed

Regards!

...JRF...
Craig Rants
Honored Contributor

Re: VI string manipulation

As mentioned sed is your answer

sed 's/BOB/FRANK/g' filename > filename2

If you want to create a lot of changes the create a sed script file called sedfile

s/BOB/FRANK/g
S/CRAIG/JOE/g

then call it from the command line

sed -f sedfile filename > filename2

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Craig Rants
Honored Contributor

Re: VI string manipulation

Also if you wanted to do your change while vi'ing the file

:%s/BOB/Frank/g

Make sure you have escaped command mode before entering this.

C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Jim Booker
Advisor

Re: VI string manipulation

Same result with awk:

awk '{gsub(/BOB/, FRANK, $0);print}' filename > filename.new