- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Find a specfic line in a file and replace it t...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:03 AM
11-01-2007 03:03 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:06 AM
11-01-2007 03:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:12 AM
11-01-2007 03:12 AM
Re: Find a specfic line in a file and replace it totally
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:15 AM
11-01-2007 03:15 AM
Re: Find a specfic line in a file and replace it totally
my_keyword = yy
my_keyword=yy
my_keyword =yy
etc.
Scott
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:21 AM
11-01-2007 03:21 AM
Re: Find a specfic line in a file and replace it totally
sed s/findvalue/newvalue filename > newfile
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:24 AM
11-01-2007 03:24 AM
Re: Find a specfic line in a file and replace it totally
Scott
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:31 AM
11-01-2007 03:31 AM
Re: Find a specfic line in a file and replace it totally
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:38 AM
11-01-2007 03:38 AM
Re: Find a specfic line in a file and replace it totally
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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:45 AM
11-01-2007 03:45 AM
Re: Find a specfic line in a file and replace it totally
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 03:56 AM
11-01-2007 03:56 AM
Re: Find a specfic line in a file and replace it totally
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2007 06:04 AM
11-01-2007 06:04 AM
Re: Find a specfic line in a file and replace it totally
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2007 02:33 AM
11-02-2007 02:33 AM
Re: Find a specfic line in a file and replace it totally
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2007 04:42 AM
11-02-2007 04:42 AM
Re: Find a specfic line in a file and replace it totally
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2007 04:53 AM
11-02-2007 04:53 AM
Re: Find a specfic line in a file and replace it totally
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.