Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have given a string and a file. I have to delete between the first 30 lines the ones having the given string. Tried something like sed -i '/pattern/1,30d' filename Yet it gives errors. More specifically what I want is sed to analyze only the first 30 lines, and if it finds the pattern given, to delete the line.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

At least with GNU sed, it appears to be possible to do

sed '1,30{/pattern/d}' file

A scaled-down test: given file as

some line
pattern
other line
some line
pattern
other line

then

$ sed '1,3{/pattern/d}' file
some line
other line
some line
pattern
other line
share|improve this answer
    
Worked good :D now just have to quiet it and ready :) –  user71884 Jun 8 at 18:14

You can combine sed with head and tail:

( head -n30 file | sed '/pattern/d' ; tail -n+31 file ) > newfile

or, use Perl:

perl -i~ -ne 'print if $. > 30 or not /pattern/' file
share|improve this answer
    
Trying right now, I'll let you know, if it worked :) –  user71884 Jun 8 at 17:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.