LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2009, 06:54 AM   #1
kbmukesh
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Rep: Reputation: 0
Delete a set of lines after finding a text


Hi All,

I need to delete a set of lines below the search word.

For Ex, i have file similar to this:

cat
1
2
3
4
6
rat
a
b
y
r
e
cat
y
u
t
bat
u
t
e
w

in this case i need to search for cat and delete 3 lines immediately below that. The result must look like

4
6
rat
a
b
y
r
e
bat
u
t
e
w
 
Old 06-16-2009, 07:07 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Homework??

Take a look at the address range syntax for SED (In the man page or the official manual at http://www.gnu.org/software/sed/manual/)

example:
sed -n '/dog/,+3p' filename

prints only the first line containing "dog", + the next 3 lines
 
Old 06-16-2009, 07:24 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '/cat/{ for(i=1;i<=3;i++) getline; next}1' file
 
Old 06-16-2009, 08:23 AM   #4
kbmukesh
LQ Newbie
 
Registered: Jun 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks ghostdog74

ghostdog74,

Thanks a lot for your quick reply & solution. It really works
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 10:40 AM
how to delete two adjecent lines in a text file wangxinmco Linux - Newbie 3 01-10-2008 02:23 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration