Please use ***
[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do
not use quote tags, bolding, colors, or other fancy formatting.
Another option in
sed is to use a nested command and eliminate that last, unwanted line.
Code:
searchstring="string"
sed -n "1,/$searchstring/ { /$searchstring/d ; p}" infile
(Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.)
Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
Yet another option is to use
ed, which has a useful negative line indexing ability:
Code:
printf '%s\n' "1,/${searchstring//\//\/}/-1p" | ed -s infile
It will fail if the matching string occurs on the first line, however.
Notice that I also added a change to the variable. If the string happens to contain a "
/", then it will conflict with the matching delimiter. The "
${SEARCHSTRING//\//\/}"
parameter substitution will add backslashes to any that exist, making it safe for all strings.
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)