LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 03-09-2012, 11:12 AM   #1
linux_kung_fu
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Rep: Reputation: 0
sed: global search and replace if a string isn't anywhere in that line


Having an issue with finding how to do a global search/replace if a string isn't found on that line.

I've got thousands of files where I need to search/replace one IP for another, but only if a specific string isn't also on that line.

Any ideas?
 
Old 03-09-2012, 11:21 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Do you need a sed solution or will awk do?

An awk solution:
Code:
awk '!/string/ { sub(/10.0.0.1/,"254.254.254.254")} { print }' infile
Example run:
Code:
$ cat infile
10.0.0.1 string
172.16.0.1
192.168.0.1
10.0.0.1

$ awk '!/string/ { sub(/10.0.0.1/,"254.254.254.254")} { print }' infile
10.0.0.1 string
172.16.0.1
192.168.0.1
254.254.254.254
Hope this helps.
 
1 members found this post helpful.
Old 03-09-2012, 11:37 AM   #3
linux_kung_fu
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: 0
awk would be fine, however that appears to write that to stdout. How would I get it to commit the changes to 'infile'? Thanks!
 
Old 03-09-2012, 11:43 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,
Quote:
Originally Posted by linux_kung_fu View Post
awk would be fine, however that appears to write that to stdout. How would I get it to commit the changes to 'infile'? Thanks!
Awk doesn't have a -i option like sed does, you need an extra command:
Code:
awk '!/string/ { sub(/10.0.0.1/,"254.254.254.254")} { print }' infile > outfile
mv outfile infile
The awk command puts its output in outfile, the mv command moves it to infile (the original will be lost!!).

Can also be written on one line:
Code:
awk '!/string/ { sub(/10.0.0.1/,"254.254.254.254")} { print }' infile > outfile ; mv outfile infile
Hope this helps.
 
Old 03-09-2012, 11:49 AM   #5
linux_kung_fu
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: 0
Thank you, didn't see an '-i' equivalent. Probably best that I write it to another file first and make sure I'm doing what I want done properly then mv things over. Thanks.
 
Old 03-09-2012, 11:53 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Actually GNU awk can edit the file in place (I think it uses the same approach explained by druuna but hidden from the user). Example:
Code:
awk '!/string/ { sub(/10.0.0.1/,"254.254.254.254")} { print > FILENAME }' infile
See the add-on in blue, where FILENAME is an internal awk variable. Again the original file is lost.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM
sed multi-line search/replace woes djmm Programming 8 03-17-2009 06:25 AM
search and replace with multi line string kambrish Programming 5 04-28-2008 07:02 AM
global search and replace? jkcunningham Linux - General 5 03-15-2006 03:43 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration