LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-15-2013, 09:12 AM   #1
KRevotsk
LQ Newbie
 
Registered: Apr 2013
Posts: 5

Rep: Reputation: Disabled
Search for a pattern and if it exists change some other text on the line


I am very new to sed and cannot seem to get my command to work. I am trying to search each line for the value 'onnection' and if it exists anywhere in the line change Abc to Xyz and write the line to a new file which is a parameter passed to the shell. My code currently looks like this
file="first_file.exp"
sed -n "/onnection/p" $file|sed -e "s/_Abc/_Xyz/g" $file > "$file_test1_parsed"

What am I doing wrong? It is changing all occurrences of Abc not just the lines that have connect in them.

TIA,
Kathy
 
Old 04-15-2013, 09:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
You don't need 2 sed parts, have a look at this:
Code:
sed '/onnection/s/Abc/Xyz/g' $file
The blue part selects lines with onnection in them and if so Abc is turned into Xyz

You can tell sed to use line or pattern ranges as well:
Code:
sed '4,$s/X/Y/g'      # Change X to Y from line 4 to last line
sed '/strngA/,/stringB/s/X/Y/g'      # change X to Y from line containing stringA to line containing stringB
These links might help:

Sed resources:
 
Old 04-15-2013, 09:35 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
What's the actual error?

Try this version:
Code:
file="first_file.exp"
sed -n "/onnection/p" "$file" | sed -e "s/_Abc/_Xyz/g" > "${file}_test1_parsed"
Edit: I think you don't have to pass the file on the second sed command.

Last edited by konsolebox; 04-15-2013 at 09:36 AM.
 
Old 04-15-2013, 09:56 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
@konsolebox: Why 2 piped sed commands in the first place?
 
Old 04-15-2013, 10:47 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@druuna I missed your post as I had my focus on the variables and jumped quickly on it sorry. After making a quick notice I just made a quick edit of my answer.

Anyway I think you should adjust the command to this:
Code:
sed -n '/onnection/{ s/Abc/Xyz/g; p; }' "$file"
I don't think the OP wants the other lines as described.
 
Old 04-15-2013, 10:54 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
And I focussed too much on the range stuff.

If only the lines need to be printed that contain onnection, it can be done like this (simpler?) as well:
Code:
sed -n '/onnection/s/Abc/Xyz/gp' $file
 
Old 04-15-2013, 11:00 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by druuna View Post
If only the lines need to be printed that contain onnection, it can be done like this (simpler?) as well:
Code:
sed -n '/onnection/s/Abc/Xyz/gp' $file
I see. I don't really try to remember that s accepts p subcommand, so I just chose the 'g; p;', but I actually had the idea before I modified it. Just to be safe.
 
Old 04-15-2013, 12:36 PM   #8
KRevotsk
LQ Newbie
 
Registered: Apr 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks all. Great suggestions! I do want all the lines in the file not just the ones I have changed.
 
Old 04-15-2013, 03:31 PM   #9
KRevotsk
LQ Newbie
 
Registered: Apr 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
What would I change in sed -n '/onnection/s/Abc/Xyz/gp' $file to get all the lines from $file to newfile with my changes?
 
Old 04-15-2013, 03:39 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Sed has a -i for in place changes (changes the original file):
Code:
sed -i -n '/onnection/s/Abc/Xyz/gp' $file

# or, create backup first
sed -i.bak -n '/onnection/s/Abc/Xyz/gp' $file
If you want to put the output in a different file:
Code:
sed -n '/onnection/s/Abc/Xyz/gp' $file > $newfile
 
Old 04-16-2013, 08:31 AM   #11
KRevotsk
LQ Newbie
 
Registered: Apr 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
I tried -i but I get sed: illegal option -- i
When I use the syntax suggested above sed -n '/onnection/s/Abc/Xyz/gp' $file > $newfile
all I get in newfile are the changed lines and I want newfile to contain the whole file containing the changed and unchanged lines.
 
Old 04-16-2013, 08:39 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by KRevotsk View Post
What would I change in sed -n '/onnection/s/Abc/Xyz/gp' $file to get all the lines from $file to newfile with my changes?
My suggestion was actually to just print the lines that contain the pattern. If you want to include other lines as well, you could just use druuna's first post:
Code:
sed '/onnection/s/Abc/Xyz/g' "$file" > "$newfile"
 
Old 04-16-2013, 09:05 AM   #13
KRevotsk
LQ Newbie
 
Registered: Apr 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Got it! Thanks all. I am all set.
 
Old 04-16-2013, 09:10 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Welcome. Glad I was able to help, miss
 
Old 04-18-2013, 02:23 PM   #15
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
By the way, as for the actual error in the OP:

Code:
sed -n "/onnection/p" $file | sed -e "s/_Abc/_Xyz/g" $file > "$file_test1_parsed"
The first sed is reading from the file, and then the second sed is also reading from the same file. The piped data between them is being ignored. Remove the "$file" argument from the second command and that should fix it.

(Although as shown you you only really need a single command.)


PS: Don't forget to quote all of your variables.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
[url]http://mywiki.wooledge.org/Quotes[url]

Edit:

Quote:
I tried -i but I get sed: illegal option -- i
Only the gnu version of sed included in most Linux distros has the -i option. You're apparently using a different implementation that doesn't provide it. Check your man page.

Last edited by David the H.; 04-18-2013 at 02:27 PM. Reason: as stated
 
  


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
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
multiple pattern search in a single file line by line saheervc Linux - Newbie 2 09-01-2010 11:45 PM
search for a pattern and comment the line swaroop.tata Programming 2 09-04-2009 07:23 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM


All times are GMT -5. The time now is 11:31 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration