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.

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
  • Worked good :D now just have to quiet it and ready :) – MEZesUBI Jun 8 '14 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
  • Trying right now, I'll let you know, if it worked :) – MEZesUBI Jun 8 '14 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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