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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-25-2008, 05:14 PM
|
#1
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Rep:
|
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?
|
|
|
|
03-25-2008, 06:12 PM
|
#2
|
|
Member
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585
Rep:
|
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
|
|
|
|
03-25-2008, 06:23 PM
|
#3
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Original Poster
Rep:
|
Quote:
Originally Posted by cmnorton
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. 
|
|
|
|
03-25-2008, 06:39 PM
|
#4
|
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Except it doesn’t have to be so complicated:
Code:
sed -e '/gah/!s/foo/oof/g'
|
|
|
|
03-25-2008, 06:49 PM
|
#5
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Original Poster
Rep:
|
Quote:
Originally Posted by osor
Except it doesn’t have to be so complicated:
Code:
sed -e '/gah/!s/foo/oof/g'
|
well that's even prettier. Thanks.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:21 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|