Operating System - Linux
1829110 Members
12337 Online
109986 Solutions
New Discussion

Re: Find a specfic line in a file and replace it totally

 
SOLVED
Go to solution
Scott Lindstrom_2
Regular Advisor

Find a specfic line in a file and replace it totally

I have been searching these forums for a while but can't find a solution to what I am looking to do.

In a script I need to find a line in file like:
my_keyword = xx
(where 'my_keyword' is a hard-coded search string but xx is anything)

but skip the replace if the line is commented out, eg:

# my_keyword = yy

and then totally replace the line, eg:

my_keyword = 55

I have a perl replace in-place example that I'd like to use but most examples I see are along the lines of find 'aaa' and replace it with 'bbb' which is not quite what I want.

Any ideas using any method would be appreciated.

Scott
14 REPLIES 14
Hasan  Atasoy
Honored Contributor

Re: Find a specfic line in a file and replace it totally


hi ;

cat file | tr -s " my_keyword = yy" " my_keyword = xx" > new_file
mv new_file old_file


Hasan
Scott Lindstrom_2
Regular Advisor

Re: Find a specfic line in a file and replace it totally

Hasan -

Thanks for the reply but my problem is 'xx' can be anything (and I'm not even sure it's always two characters long). If this was a hardcoded value that would simplify it a lot.

Scott
Scott Lindstrom_2
Regular Advisor

Re: Find a specfic line in a file and replace it totally

Plus I'd like to allow for the following cases in the find (if I can):

my_keyword = yy

my_keyword=yy

my_keyword =yy

etc.

Scott
Steven E. Protter
Exalted Contributor

Re: Find a specfic line in a file and replace it totally

Shalom,

sed s/findvalue/newvalue filename > newfile


SEP

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Scott Lindstrom_2
Regular Advisor

Re: Find a specfic line in a file and replace it totally

Steven - Would you be able to help me craft 'findvalue' to match my criteria? That is the crux of my problem.

Scott
Hasan  Atasoy
Honored Contributor

Re: Find a specfic line in a file and replace it totally

a bit dummy solution :=

cat file | tr -s "my_keyword = yy" "my_keyword = xx" | tr -s "my_keyword= yy" "my_keyword= xx" |tr -s "my_keyword =yy" "my_keyword =xx" > new_file
mv new_file old_file

Hasan
James R. Ferguson
Acclaimed Contributor

Re: Find a specfic line in a file and replace it totally

Hi Scott:

Perhaps you want:

# perl -pe 'next if /^\s*#/;s/(my_keyword\s*=\s*)(.+)/$1newvalue/' file

...where, in this case, the string "newvalue" is substituted for "xx" in your example.

Regards!

...JRF...
Scott Lindstrom_2
Regular Advisor

Re: Find a specfic line in a file and replace it totally

James -

I think that gets me closer. Here is my test file with the actual keyword:


# stats_change_threshold = 20

stats_change_threshold = 20

In this line I want to change 20 to 53, so I tried:

perl -pe 'next if /^\s*#/;s/(stats_change_threshold\s*=\s*)(.+)/$153/' testfile

But that does not update the file as I need. What am I missing?
James R. Ferguson
Acclaimed Contributor
Solution

Re: Find a specfic line in a file and replace it totally

Hi Scott:

# perl -pe 'next if /^\s*#/;s/(stats_change_threshold\s*=\s*)(.+)/${1}53/' testfile

...Notice the curly braces around the original $1.

Regards!

...JRF...
Scott Lindstrom_2
Regular Advisor

Re: Find a specfic line in a file and replace it totally

James -

That does it! I just updated it to do an inplace update:

perl -pe 'next if /^\s*#/;s/(stats_change_threshold\s*=\s*)(.+)/${1}53/' -i testfile testfile

and I got the correct results:

#cat testfile
# stats_change_threshold = 20
stats_change_threshold = 53

Thanks,
Scott
Sandman!
Honored Contributor

Re: Find a specfic line in a file and replace it totally

You could use sed(1) for the doing the same thing except that the file won't change in place.

# sed 's/^\( *stats_change_threshold *\)=\( *\)20/\1=\253/g' file

...and to update the file in place use ex(1) as follows:

# ex -s +"%s/^\( *stats_change_threshold *\)=\( *\)20/\1=\253/ | wq" file
Doug O'Leary
Honored Contributor

Re: Find a specfic line in a file and replace it totally

Me, personally, I like the perl inline stuff:

perl -i -ple 's/^(my_keyword\s*=\s*).*/\155/g' ${file}

As always, try it out w/o the -i arg and verify it's doing what you expect:

perl -ple 's/^(my_keyword\s*=\s*).*/\155/g' ${file} | grep -i my_keyword

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Victor Fridyev
Honored Contributor

Re: Find a specfic line in a file and replace it totally

Hi,

As it was mentioned, sed provides the simplest way:

lookfor="my_keyword=xx"
replacewith="my_keyword=yy"
cat inputfile|sed "s/^$lookfor$/$replaceeith/" > outputfile

Please pay your attention on double quotes

HTH
Entities are not to be multiplied beyond necessity - RTFM
Hein van den Heuvel
Honored Contributor

Re: Find a specfic line in a file and replace it totally

Victor, re-read the original request.
Best I can tell, your solution does not even come close !?
Take of that crown for 5 minutes, and go stand in the corner!

Cheers,
Hein.