且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 bash/sed 脚本删除文本文件的第一行?

更新时间:2023-12-05 19:30:22

试试 :

tail -n +2 "$FILE"

-n x:只打印最后的 x 行.tail -n 5 会给你输入的最后 5 行.+ 符号会反转参数并使 tail 打印除第一行 x-1 之外的任何内容.tail -n +1 将打印整个文件,tail -n +2 除了第一行之外的所有内容,等等.

-n x: Just print the last x lines. tail -n 5 would give you the last 5 lines of the input. The + sign kind of inverts the argument and make tail print anything but the first x-1 lines. tail -n +1 would print the whole file, tail -n +2 everything but the first line, etc.

GNU tailsed 快得多.tail 在 BSD 上也可用,并且 -n +2 标志在两个工具中是一致的.检查 FreeBSDOS X 手册页了解更多信息.

GNU tail is much faster than sed. tail is also available on BSD and the -n +2 flag is consistent across both tools. Check the FreeBSD or OS X man pages for more.

尽管如此,BSD 版本可能比 sed 慢得多.我想知道他们是如何做到的;tail 应该一行一行地读取文件,而 sed 执行非常复杂的操作,包括解释脚本、应用正则表达式等.

The BSD version can be much slower than sed, though. I wonder how they managed that; tail should just read a file line by line while sed does pretty complex operations involving interpreting a script, applying regular expressions and the like.

注意:您可能会想使用

# THIS WILL GIVE YOU AN EMPTY FILE!
tail -n +2 "$FILE" > "$FILE"

但这会给你一个空文件.原因是重定向(>)发生在tail被shell调用之前:

but this will give you an empty file. The reason is that the redirection (>) happens before tail is invoked by the shell:

  1. Shell 截断文件 $FILE
  2. Shell 为 tail
  3. 创建一个新进程
  4. Shell 将 tail 进程的标准输出重定向到 $FILE
  5. tail 从现在为空的 $FILE
  6. 读取
  1. Shell truncates file $FILE
  2. Shell creates a new process for tail
  3. Shell redirects stdout of the tail process to $FILE
  4. tail reads from the now empty $FILE

如果你想删除文件中的第一行,你应该使用:

If you want to remove the first line inside the file, you should use:

tail -n +2 "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE"

&& 将确保文件在出现问题时不会被覆盖.

The && will make sure that the file doesn't get overwritten when there is a problem.