LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 10: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, 10:21 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 10: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, 10:43 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 10: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, 10:53 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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



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 11:05 AM
sed multi-line search/replace woes djmm Programming 8 03-17-2009 05:25 AM
search and replace with multi line string kambrish Programming 5 04-28-2008 06:02 AM
global search and replace? jkcunningham Linux - General 5 03-15-2006 02:43 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:58 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
Open Source Consulting | Domain Registration