System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

find string and replace with variable value

 
diwakar_4
Frequent Advisor

find string and replace with variable value

Hi,


I have one file contains headera.

XHEADER 200800901001 REC
as,34,rttt
as,35,rttt
as,36,rttt
FOOTER
___________________________________

Now i have one variable:

count=10

Ihave to replace HEADER 'REC' with 10

then HEADER WILL be
_____________________________
XHEADER 200800901001 10
as,34,rttt
as,35,rttt
as,36,rttt
FOOTER
_______________________________

Can some please help me?

Thanks

3 REPLIES
Mark McDonald_2
Trusted Contributor

Re: find string and replace with variable value

Use sed:

sed /REC/$count/.. file

not sure of the exact syntax.
James R. Ferguson
Acclaimed Contributor

Re: find string and replace with variable value

Hi:

You can use:

# sed -e "s/REC/$count/" file > file.new
# mv file.new file

Note the use of the double quotes.

You could also use Perl and update your file, in-place, in one "operation":

# perl -pi -e "s/REC/$count/" file

Regards!

...JRF...

Re: find string and replace with variable value

If you only want to change REC in lines that have XHEADER you can change JRF's sed:
sed -e "/XHEADER/s/REC/$count/" file > file.new