从Bash中的文件中删除最后一行

本文提供了在Bash中删除文件最后一行的各种方法,包括适用于大文件的高效解决方案,强调了使用技巧以避免文件读取和重写的问题。请注意,某些操作会直接修改文件,建议先备份。
摘要由CSDN通过智能技术生成

本文翻译自:Remove the last line from a file in Bash

I have a file, foo.txt , containing the following lines: 我有一个文件foo.txt ,包含以下行:

a
b
c

I want a simple command that results in the contents of foo.txt being: 我想要一个简单的命令,导致foo.txt的内容为:

a
b

#1楼

参考:https://stackoom.com/question/KU0o/从Bash中的文件中删除最后一行


#2楼

This is by far the fastest and simplest solution, especially on big files: 这是迄今为止最快,最简单的解决方案,尤其是在大文件上:

head -n -1 foo.txt > temp.txt ; mv temp.txt foo.txt

if You want to delete the top line use this: 如果你想删除顶行使用这个:

tail -n +2 foo.txt

which means output lines starting at line 2. 这意味着输出线从第2行开始。

Do not use sed for deleting lines from the top or bottom of a file -- it's very very slow if the file is large. 不要使用sed从文件的顶部或底部删除行 - 如果文件很大,它会非常慢。


#3楼

I had trouble with all the answers here because I was working with a HUGE file (~300Gb) and none of the solutions scaled. 我在这里得到了所有答案的麻烦,因为我正在处理一个巨大的文件(~300Gb)并且没有任何解决方案缩放。 Here's my solution: 这是我的解决方案:

dd if=/dev/null of=<filename> bs=1 seek=$(echo $(stat --format=%s <filename> ) - $( tail -n1 <filename> | wc -c) | bc )

In words: Find out the length of the file you want to end up with (length of file minus length of length of its last line, using bc ) and, set that position to be the end of the file (by dd ing one byte of /dev/null onto it). 单词:找出你想要最终得到的文件的长度(文件长度减去最后一行长度的长度,使用bc ),并将该位置设置为文件的末尾(通过dd ing一个字节) /dev/null到它上面)。

This is fast because tail starts reading from the end, and dd will overwrite the file in place rather than copy (and parse) every line of the file, which is what the other solutions do. 这很快,因为tail从末尾开始读取,而dd将覆盖文件而不是复制(和解析)文件的每一行,这是其他解决方案所做的。

NOTE: This removes the line from the file in place! 注意:这将从文件中删除行! Make a backup or test on a dummy file before trying it out on your own file! 在您自己的文件上尝试之前,对虚拟文件进行备份或测试!


#4楼

To remove the last line from a file without reading the whole file or rewriting anything , you can use 要从文件中删除最后一行而不读取整个文件或重写任何内容 ,您可以使用

tail -n 1 "$file" | wc -c | xargs -I {} truncate "$file" -s -{}

To remove the last line and also print it on stdout ("pop" it), you can combine that command with tee : 要删除最后一行并将其打印在stdout上(“弹出”它),您可以将该命令与tee结合使用:

tail -n 1 "$file" | tee >(wc -c | xargs -I {} truncate "$file" -s -{})

These commands can efficiently process a very large file. 这些命令可以有效地处理非常大的文件。 This is similar to, and inspired by, Yossi's answer, but it avoids using a few extra functions. 这与Yossi的回答类似,并受其启发,但它避免使用一些额外的功能。

If you're going to use these repeatedly and want error handling and some other features, you can use the poptail command here: https://github.com/donm/evenmoreutils 如果您要反复使用这些并想要错误处理和其他一些功能,可以在这里使用poptail命令: https//github.com/donm/evenmoreutils


#5楼

Mac Users Mac用户

if you only want the last line deleted output without changing the file itself do 如果你只想删除最后一行输出而不改变文件本身呢

sed -e '$ d' foo.txt

if you want to delete the last line of the input file itself do 如果你想删除输入文件本身的最后一行呢

sed -i '' -e '$ d' foo.txt


#6楼

awk "NR != `wc -l < text.file`" text.file |> text.file

这个片段可以解决这个问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值