LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Delete a set of lines after finding a text (https://www.linuxquestions.org/questions/linux-newbie-8/delete-a-set-of-lines-after-finding-a-text-733310/)

kbmukesh 06-16-2009 06:54 AM

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

pixellany 06-16-2009 07:07 AM

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

ghostdog74 06-16-2009 07:24 AM

Code:

awk '/cat/{ for(i=1;i<=3;i++) getline; next}1' file

kbmukesh 06-16-2009 08:23 AM

Thanks ghostdog74
 
ghostdog74,

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


All times are GMT -5. The time now is 08:49 PM.