如何使用 bash/sed 脚本删除文本文件的第一行? [英] How can I remove the first line of a text file using bash/sed script?

查看:28
本文介绍了如何使用 bash/sed 脚本删除文本文件的第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 bash 脚本从一个巨大的文本文件中重复删除第一行.

I need to repeatedly remove the first line from a huge text file using a bash script.

现在我正在使用 sed -i -e "1d" $FILE - 但是删除需要大约一分钟.

Right now I am using sed -i -e "1d" $FILE - but it takes around a minute to do the deletion.

有没有更有效的方法来实现这一点?

Is there a more efficient way to accomplish this?

推荐答案

试试 :

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.

这篇关于如何使用 bash/sed 脚本删除文本文件的第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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