how to find pattern and discard lines before it?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find pattern and discard lines before it?
# 1  
Old 05-25-2012
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
# 2  
Old 05-25-2012
Some things to ponder

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

would tell you the first line number with that phrase

Code:
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
Code:
sed -e '1,2d' sample.txt > newfile.txt

would copy sample.txt to newfile.txt, skipping the first two lines
# 3  
Old 05-25-2012
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:

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

But I can't make the variable work in the next part:
Code:
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
# 4  
Old 05-25-2012
Try sed "1,${LINE}d". Alternatively, try:
Code:
sed -n '/PLASTICS THAT EXPIRE/,$p' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-25-2012
Whoa, that did it in one line. Thanks, Scroot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find pattern; grep n lines before and after

Hi, I need help to grep a specific part of a log file (bold). 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: } 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: extensionContainer <Not Present> 24/2/2017-16:57:17.056... (8 Replies)
Discussion started by: vasil
8 Replies

2. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

Discard part of a file based on a pattern ---

I have the file: s3_T0(2) Pos "1" "2" s1_T1(2) Pos "1" "2" --- 0 0 1 0 0 1 1 1 --- 1 2 "tau0" 1 2 "h10" I want to patternmatch on --- and get only the third part i.e. 1 2 "tau0" 1 2 "h10" I wanted to start simple but even something like (5 Replies)
Discussion started by: eagle_fly
5 Replies

5. Shell Programming and Scripting

[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills": I need to find a pattern /VEL/ in an input file that looks like this: 1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736 ... (3 Replies)
Discussion started by: origamisven
3 Replies

6. Shell Programming and Scripting

Gawk Find Pattern Print Lines Before and After

Using grep I can easily use: cvs log |grep -iB 10 -A 10 'date: 2013-10-30' to display search results and 10 lines before and after. How can this be accompished using gawk? (4 Replies)
Discussion started by: metallica1973
4 Replies

7. Shell Programming and Scripting

Number some lines discard others?

Hi, I'd like to do an operation on text with a format like this this line shall be numbered this line shall not be numbered this line shall also be numbered this line shall not not be numbered And I want an output like this 1 this line shall be numbered this line... (6 Replies)
Discussion started by: jeppe83
6 Replies

8. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

9. Shell Programming and Scripting

Find pattern a delete previous 5 lines

Hi guys, i have the follow problem i need to delete 10 row before the pattern and 1 after and the pattern row itself. file looks like: frect 9.8438 25.8681 10.625 25 . dynprop \ (# \ (call fox_execute(__self))) \ (FOX_VAR_29 \ ... (4 Replies)
Discussion started by: EjjE
4 Replies

10. Shell Programming and Scripting

Joining lines and find pattern

I have a file that does not have much formatting. I would like every time it finds a tag (something that starts with < and end with > like <Tag1> ) to be at the beginning of the line and joined with the next line. I tried sed with append and can not get it right and also hold and get in sed... (4 Replies)
Discussion started by: quixoticking11
4 Replies
Login or Register to Ask a Question