Delete the lines before the first instance of the keyword


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete the lines before the first instance of the keyword
# 1  
Old 07-28-2009
Delete the lines before the first instance of the keyword

I have my data something like this. I want to delete all the lines before the frist instance of the key word 'ravi kumar'

Code:
aaa
bbbbbb cccccc
ddddd eeeee
1234 ravi kumar
aaaaaa vvvvvvv
5678 ravi kumar
rrrrrrr mmmmmmm

I want the output as follows.

Code:
1234 ravi kumar
aaaaaa vvvvvvv
5678 ravi kumar
rrrrrrr mmmmmmm

Please help me.
# 2  
Old 07-28-2009
Code:
awk '/ravi kumar/,/*/ {print $0}'  yourfle

# 3  
Old 07-28-2009
How about this?


Code:
 
 sed -n '/ravi kumar/,/*/p' file

# 4  
Old 07-28-2009
Sed is working perfectly where as there seems to be a syntax issue with awk command above.
Thanks a lot for your help.
# 5  
Old 07-28-2009
Code:
sed -n '/ravi kumar/,$p' file

Code:
awk '/ravi kumar/,i++' file

Jean-Pierre.
# 6  
Old 07-28-2009
Quote:
Originally Posted by aigles
Code:
awk '/ravi kumar/,i++' file

Jean-Pierre.
Jean-Pierre,

Your awk command ignores the last line.

Another approach with awk:

Code:
awk '/ravi kumar/{p=1}p' file

Regards
# 7  
Old 07-28-2009
Quote:
Originally Posted by Franklin52
Jean-Pierre,

Your awk command ignores the last line.
Oups! you're right Franklin.

Code:
awk '/ravi kumar/,p' file

Input:
Code:
aaa
bbbbbb cccccc
ddddd eeeee
1234 ravi kumar
aaaaaa vvvvvvv
bbbbbbb wwwwwwww
ccccccccc zzzzzzzzzz
5678 ravi kumar
rrrrrrr mmmmmmm END

Output:
Code:
1234 ravi kumar
aaaaaa vvvvvvv
bbbbbbb wwwwwwww
ccccccccc zzzzzzzzzz
5678 ravi kumar
rrrrrrr mmmmmmm END

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

extract lines from text after keyword

I have a text and I want to extract the 4 lines following a keyword! For example if I have this text and the keyword is AAA hello helloo AAA one two three four helloooo hellooo I want the output to be one two three four (7 Replies)
Discussion started by: stekanius
7 Replies

2. Shell Programming and Scripting

Displaying all the lines starting with some keyword

consider the contents of a file has many stuff including few stuff that i need.. so i perfromed the below function cat filename | grep "ALTER TABLE" its output is as shown below . . . . . SET @sql:=CONCAT('ALTER TABLE RecordMixProfile AUTO_INCREMENT=', @maxId) ; SET... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. Shell Programming and Scripting

Extract Lines Containg a Keyword

Hi , I have two files, say KEY_FILE and the MAIN_FILE. I am trying to read the KEY_FILE which has only one column and look for this column data in the MAIN_FILE to extract all the rows that have this key. I have written a script to do so, but somehow it is not returning all the rows ( It... (4 Replies)
Discussion started by: Sheel
4 Replies

4. Shell Programming and Scripting

Using sed to delete a line having a particular keyword

Hi Geeks :b:, I need to delete a line from file that contains a particular keyword. I had read in some forum of unix.com that below code could be used sed "/$titlesearch/d" movielist >tmp mv tmp movielist But my file contains lines which contain slashes (/) FOr eg: /etc/movie/title/... (5 Replies)
Discussion started by: ajincoep
5 Replies

5. Shell Programming and Scripting

sed/awk: Delete matching words leaving only the first instance

I have an input text that looks like this (comes already sorted): on Caturday 22 at 10:15, some event on Caturday 22 at 10:15, some other event on Caturday 22 at 21:30, even more events on Funday 23 at 11:00, yet another event I need to delete all the matching words between the lines, from... (2 Replies)
Discussion started by: GrinningArmor
2 Replies

6. Shell Programming and Scripting

How can i delete a keyword containing XYZ in unix

Hi all, I am trying to remove the words which has XYZ as a prt of that. My input file is something like this : PHNDAZLF-UPS-XYZ' aaaaaaa bbbbb ADFRTEJKS-XYZ cccccccc ddddddd rrrrrr SGETHEHDJ-ABC-RXY' hhhhh ttttt' kkkk FHJSKSJDKD-XXX-YYY Output expected is : aaaaaaa... (7 Replies)
Discussion started by: rdhanek
7 Replies

7. Shell Programming and Scripting

search for keyword in subsequent lines and delete the second line

I have my data something like this I need to search for the keyword yyyy in the susequent lines and if it is present, delete the second line with keyword. In other words, if a keywords is found in two subsequent lines delete the second line. input data: aaaa bbbbb cccc dddd xxxx... (4 Replies)
Discussion started by: rdhanek
4 Replies

8. Shell Programming and Scripting

Delete the lines after the last instance of the keyword

I have my input sometyhing like this aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar rrrrrrr mmmmmmm I want the output as follows. aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar (2 Replies)
Discussion started by: rdhanek
2 Replies

9. Shell Programming and Scripting

How can i delete a keyword starting with x in unix

I am trying to delete key word starting with x in a unix text file. example, I am trying to delete the words like xaa,xabxbb,xbd and so on.... my input file is some thing like this xaaa w 1234 5678 rwsd ravi xw123 xbc3 ohrd want to delete words xaaa,xw123 and xbc3 from the above... (10 Replies)
Discussion started by: rdhanek
10 Replies

10. Shell Programming and Scripting

Cut lines before keyword

Hi, How to cut all lines in file before keyword? from 1 2333214 word ...... some text 2 234343 234234 word ...... some text 3 234324 324 3234 word ...... some text to 1 2333214 2 234343 234234 3 234324 324 3234 (4 Replies)
Discussion started by: Trump
4 Replies
Login or Register to Ask a Question