2

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.

2 Answers 2

4

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
1
  • Worked good :D now just have to quiet it and ready :)
    – MEZesUBI
    Commented Jun 8, 2014 at 18:14
1

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
1
  • Trying right now, I'll let you know, if it worked :)
    – MEZesUBI
    Commented Jun 8, 2014 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .