LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 05-29-2007, 01:33 PM   #1
RobHill
LQ Newbie
 
Registered: Mar 2007
Posts: 7

Rep: Reputation: 0
Using sed in a shell script


In one of my last threads I asked a question about modifying a file, by removing everything in a line up to a certain word. Well what I got back was something similar to this :%s/^.*foo\ // What I need to know is how can I ad that to a script using sed? I have tried sed -n -e '%s/^.*foo\ //' file > newfile, however I keep getting an error "sed: -e expression #1, char 1: unknown command: '%', so is it not recognizing the '%'? and if not what should I use? Oh..and the script is not pearl.
 
Old 05-29-2007, 02:27 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

You don't need the % (no range is needed when you want to check the complete input file), if this is the complete sed statement, you also don't need the -e part. -e is used to string sed statements together. Which leaves you with this:

sed -n 's/^.*foo\ //' file > newfile

If you start working with variables inside the sed statement, you should use double quotes (") instead of singe (').

Hope this helps.
 
Old 05-29-2007, 03:08 PM   #3
RobHill
LQ Newbie
 
Registered: Mar 2007
Posts: 7

Original Poster
Rep: Reputation: 0
thanks druuna for sed question help!

You state that I don't need the -e but should use the -n. So what is the format I should use if I wanted to string sed statement? Say if I wanted to delete from the word foo to the end of the line? (opposite of my first question) example: 's/\foo*.*$//' how would I put the two together? what kind of seperator do I need to seperate the two commands or do I need a seperator at all? Thanks in advance!
 
Old 05-29-2007, 03:24 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Quote:
You state that I don't need the -e but should use the -n.
Nope, you decided to use the -n soption. I did tell you not to use the -e option.

Quote:
So what is the format I should use if I wanted to string sed statement?
Here's an example:
sed -e 's/foo/bar/g' -e '$d' -e '1,5s/this/that/' infile
The above changes every foo to bar, deletes the last line and changes the first this in a line to that if it is found in the first 5 lines of the inputfiile.

Quote:
Say if I wanted to delete from the word foo to the end of the line? (opposite of my first question) example: 's/\foo*.*$//' how would I put the two together? what kind of seperator do I need to seperate the two commands or do I need a seperator at all? Thanks in advance!
'Just' string them together with the -e option as shown in the example.

Hope this clears things up a bit.
 
Old 05-29-2007, 03:31 PM   #5
RobHill
LQ Newbie
 
Registered: Mar 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,

Thanks that does help very much!
 
  


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
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
Sed shell script question pete1234 Programming 4 08-18-2006 09:15 PM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
shell script problem on sed. chooi Programming 3 02-10-2006 11:35 PM


All times are GMT -5. The time now is 03:02 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