Delete line containing pattern in between specific range of lines

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 command b, 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, the d 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 switches
  • perldoc perlvar: Perl predefined variables

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật