Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

shell - 删除文本文件中包含特定字符串的行(Delete lines in a text file that contain a specific string)

我将如何使用sed删除文本文件中包含特定字符串的所有行?

  ask by A Clockwork Orange translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To remove the line and print the output to standard out:

(要删除行并将输出打印到标准输出,请执行以下操作:)

sed '/pattern to match/d' ./infile

To directly modify the file – does not work with BSD sed:

(要直接修改文件–不适用于BSD sed:)

sed -i '/pattern to match/d' ./infile

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

(相同,但是对于BSD sed(Mac OS X和FreeBSD)–不适用于GNU sed:)

sed -i '' '/pattern to match/d' ./infile

To directly modify the file (and create a backup) – works with BSD and GNU sed:

(要直接修改文件(并创建备份)–与BSD和GNU sed一起使用:)

sed -i.bak '/pattern to match/d' ./infile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...