2020年9月11日 星期五

[Linux 常見問題] Bash - Remove the last line from a file

 Source From Here

Question
I have a file, foo.txt, containing the following lines:
I want a simple command that results in the contents of foo.txt being:
How-To
This is by far the fastest and simplest solution, especially on big files:
// -n, --lines=[-]NUM: print the first NUM lines instead of the first 10; with the leading '-', print all but the last NUM lines of each file
# cat foo.txt
a
b
c


# head -n +2 foo.txt
a
b


# head -n -1 foo.txt
a
b


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

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. It can still work as below:
# sed '$ d' test.txt
a
b


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...