本文翻译自: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
这个片段可以解决这个问题。