巴什 - 从文件中删除最后一行 [英] Bash - remove the last line from a file

查看:191
本文介绍了巴什 - 从文件中删除最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件, foo.txt的,包含以下行:

  A
b
C

我想要一个简单的命令导致 foo.txt的的内容存在:

  A
b


解决方案

使用 GNU的sed

  SED -i'$ D'foo.txt的

-i 选项不会在旧版本存在的,所以你必须有一个临时文件使用它作为一个过滤器:

  CP foo.txt的foo.txt.tmp
SED'$ D'foo.txt.tmp> foo.txt的
RM -f foo.txt.tmp

当然,在这种情况下,你也可以使用头-n -1 而不是 SED

I have a file, foo.txt, containing the following lines:

a
b
c

I want a simple command that results in the contents of foo.txt being:

a
b

解决方案

Using GNU sed:

sed -i '$ d' foo.txt

The -i option does not exist in older versions, so you have to use it as a filter with a temporary file:

cp foo.txt foo.txt.tmp
sed '$ d' foo.txt.tmp > foo.txt
rm -f foo.txt.tmp

Of course, in that case you could also use head -n -1 instead of sed.

这篇关于巴什 - 从文件中删除最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆