LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-25-2008, 05:14 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
need sed help - how to replace all instances of X except those on lines with Y?


say I have a file:

Code:
foo bar
gah foo bar
foo foo
bar gah
foo gah
... and I want to replace all instances of "foo" with "oof" except those on lines that also have "gah". So, I would want the output to look like:

Code:
oof bar
gah foo bar
oof oof
bar gah
foo gah
Is this even possible?
 
Old 03-25-2008, 06:12 PM   #2
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
One sed Example

If I correctly understood what you wrote, this seems to work:

#!/bin/sed

/\(.*\)\(gah\)\(.*$\)/!s/foo/oof/g

I wanted to find all lines without gah, and then do a simple substitution on those lines. You could do this with something more complicated, but then you'd be getting into parts of matches, which gets more complicat
 
Old 03-25-2008, 06:23 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by cmnorton View Post
If I correctly understood what you wrote, this seems to work:

#!/bin/sed

/\(.*\)\(gah\)\(.*$\)/!s/foo/oof/g

I wanted to find all lines without gah, and then do a simple substitution on those lines. You could do this with something more complicated, but then you'd be getting into parts of matches, which gets more complicat
perfect! Thanks! That would have taken a really long time to figure out.
 
Old 03-25-2008, 06:39 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Except it doesn’t have to be so complicated:
Code:
sed -e '/gah/!s/foo/oof/g'
 
Old 03-25-2008, 06:49 PM   #5
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by osor View Post
Except it doesn’t have to be so complicated:
Code:
sed -e '/gah/!s/foo/oof/g'
well that's even prettier. Thanks.
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
file renaming question--replace multiple instances David the H. Linux - General 4 01-01-2008 12:05 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 05:27 PM


All times are GMT -5. The time now is 01:30 AM.

Main Menu
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