LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using sed in a shell script (https://www.linuxquestions.org/questions/linux-general-1/using-sed-in-a-shell-script-557504/)

RobHill 05-29-2007 01:33 PM

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.

druuna 05-29-2007 02:27 PM

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.

RobHill 05-29-2007 03:08 PM

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!

druuna 05-29-2007 03:24 PM

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.

RobHill 05-29-2007 03:31 PM

Hi,
 
Thanks that does help very much!


All times are GMT -5. The time now is 03:16 AM.