Linux删除文件某一行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29695701/article/details/87795078

Linux删除文件某一行

  • 使用命令sed
    sed -i "nd" filename

如果想要删除最后一行,但是不知道文件一共几行,可以通过 cat filename | wc -l 获取总行数,再删除,或者直接使用命令 sed -i "$d" filename 删除最后一行

例子:

$ ls
test.txt
$ more text.txt
first
second
thrid
forth
fifth
$ # 备份原文件
$ cp test.txt test.txt.bak
$ # 删除test.txt的第一行
$ sed -i "1d" test.txt
$ more test.txt
second
thrid
forth
fifth
$ # 还原文件
$ cp test.txt.bak test.txt
$ # 删除test.txt的第三行
$ sed -i "3d" test.txt
$ more test.txt
first
second
forth
fifth
$ # 还原文件
$ cp test.txt.bak test.txt
$ # 删除test.txt的最后一行
$ sed -i "$d" test.txt
$ more test.txt
first
second
thrid
forth

mac上,sed的使用方法略有不同,参见:
https://www.cnblogs.com/meitian/p/5907562.html

猜你喜欢

转载自blog.csdn.net/qq_29695701/article/details/87795078