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.
|
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. |
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!
|
Hi,
Quote:
Quote:
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:
Hope this clears things up a bit. |
Hi,
Thanks that does help very much!
|
All times are GMT -5. The time now is 03:16 AM. |