开发者

Delete line ending with a newline character in text file

开发者 https://www.devze.com 2023-01-08 20:55 出处:网络
I need to delete the same line in a large number of text files. I have been trying to use sed, but I cannot get it to delete the newline character at the end. The following successfully deletes the li

I need to delete the same line in a large number of text files. I have been trying to use sed, but I cannot get it to delete the newline character at the end. The following successfully deletes the line, but not the newline:

sed -i -e 's/VERSION:1//' *.txt
开发者_如何转开发

I have tried using the following to delete the newline also, but it does not work:

sed -i -e 's/VERSION:1\n//' *.txt

Is there anyway to specify a newline in a sed substitute command OR is there any other command line tool I can use to achieve my goal? Thank you


You can use the sed command:

sed -i -e '/VERSION:1/d'

for this.

The following transcript gives an example:

pax> echo 'hello
> goodbye
> hello again' | sed '/oo/d'
hello
hello again

You should also check whether you want to match whole lines with, for example:

sed -i -e '/^VERSION:1$/d'

since, as it stands, that will also delete lines like the following:

VERSION:10
CONVERSION:1


sed '/VERSION:1/{:q;N;s/VERSION\n//g;t q}' file
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号