how to find pattern and discard lines before it?

Hi all,

I'd like to search a file for the first occurence of the phrase "PLASTICS THAT EXPIRE" and then discard all the lines that came before it. Output the remainder to a new file. Operating system is hp-ux. I've searched for usual awk and sed one liners but can't find a solution.

Thank you,

~Scottie

cat -n sample.txt | grep "PLASTICS" | head -1 | cut -f1

would tell you the first line number with that phrase

tail -n +3 sample.txt

would start displaying the file at the 3rd line
(useful, if you knew that you wanted to skip the first 2 lines)

also

sed -e '1,2d' sample.txt > newfile.txt

would copy sample.txt to newfile.txt, skipping the first two lines

Thanks for your help. Now for the follow up (you knew there had to be more, eh?).
If I set a variable to the line number found by the cat | grep, how do I get sed or tail to recognize it? So far I've got:

LINE=$(cat -n sample.txt | grep "PLASTICS" | head -1 | cut -f1)

But I can't make the variable work in the next part:

sed -e "1,'$LINE'd" sample.txt >newfile.txt

hp-ux tells me "sed: Function 1,' 90'd cannot be parsed."

Thanks a bunch,

Scottie

Try sed "1,${LINE}d" . Alternatively, try:

sed -n '/PLASTICS THAT EXPIRE/,$p' infile
1 Like

Whoa, that did it in one line. Thanks, Scroot.