I want to completely remove lines containing a pattern ignoring first few lines. I tried:
$ sed -i '2,$s/pattern/d' file.txt
but it says unterminated's' command
.
For example I have a file:
pattern # important one, not to be removed
some line1
some line2
pattern some content
another line3
pattern another content
another line4
So, I want
pattern # important one, not to be removed
some line1
some line2
another line3
another line4
If I use sed -i '2,$s/pattern//' file.txt
then it doesn’t completely remove the line, there are characters that aren’t in pattern, even though it must be removed.
2
The easiest way to do this in sed
would probably be to skip the lines you want to ensure cannot be affected, instead of selecting the lines you want to consider, and then sub-selecting within those the ones you actually want to delete. Example:
sed '1b;/pattern/d' file.txt
Explanation:
That’s two independent rules , separated by a semicolon (;
).
-
The first has address
1
, which selects line 1 by line number, and commandb
, which, in this form, ends the current sed cycle and starts a new one.sed
‘s default behavior is to print its pattern space at the end of each cycle, before reading the next line, so this just passes the first line through. -
The second has address
/pattern/
, which selects the lines you want to delete from among those not already skipped. Of course, thed
command deletes them.
You could use any kind of address with the b
command, not just line number, and you could put any number of them at the start of the sed
expression to exclude diverse lines based on different criteria.
The s
command is for “search and replace”, and you’re missing the replace part.
The d
command takes a pattern. Two specify 2 patterns, use a {...}
block.
For example, from the numbers 1 to 20, delete numbers containing 3 or 8, but only between numbers 5 to 15
seq 20 | sed '5,15{/[38]/d;}'
1
2
3
4
5
6
7
9 <<< "8" removed
10
11
12
14 <<< "13" removed
15
16
17
18
19
20
Just use awk:
$ awk 'NR<2 || !/pattern/' file
pattern # important one, not to be removed
some line1
some line2
another line3
another line4
or if you prefer a positive condition:
$ awk 'NR>1 && /pattern/{next} {print}' file
pattern # important one, not to be removed
some line1
some line2
another line3
another line4
This might work for you (GNU sed):
sed '/pattern/{1b;d}' file
If a line contains pattern
and it is not the first line, delete it.
Or if you prefer:
sed '/pattern/{2,$!b;d}' file
If you are interested in only deleting pattern
lines after the first such line, use:
sed '/pattern/{x;//d;g}' file
This uses the hold space (a separate register) as a flag.
Use this Perl one-liner:
perl -i.bak -ne 'if ( $. > 1 ) { next if /pattern/ } print;' file.txt
The Perl one-liner uses these command line flags:
-e
: Tells Perl to look for code in-line, instead of in a file.
-n
: Loop over the input one line at a time, assigning it to $_
by default.
-i.bak
: Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak
. If you want to skip writing a backup file, just use -i
and skip the extension.
$.
: Current input line number.
See also:
perldoc perlrun
: how to execute the Perl interpreter: command line switchesperldoc perlvar
: Perl predefined variables