简体   繁体   中英

How do I delete lines of a given interval in a file containing a matching word?

I need to use sed to delete from a file only the lines containing a given word. But if my file has 50 lines, I only need to look at the first 30 lines for example.

I know that:

sed '1,30d' f.txt

will delete the first 30 lines of the file.

And:

sed '/Text/d' f.txt

will delete all the lines in the file containing the matching word "Text".

How do I combine these two to solve my problem? Thank you.

It seems that you want to delete the lines containing Text within the first 30 lines in your file.

You can say:

sed '1,30{/Text/d}' f.txt

If you wanted to delete the first 30 lines and the lines containing Text anywhere in the file, you could say:

sed '1,30d;/Text/d' f.txt

您错过了“ -i”选项:

sed -i '1,30{/Text/d}' f.txt
sed "/Text/ d;30 q" f.txt

应该更快,尤其是在大文件中, 如果您在30后丢弃行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM