在UNIX命令行中删除文件的第N行到位 [英] Remove first N lines of a file in place in unix command line

查看:372
本文介绍了在UNIX命令行中删除文件的第N行到位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个非常,非常大的文件中删除第37行。我开始尝试sed和awk,但他们似乎需要将数据复制到一个新的文件。我在寻找一个删除到位线的方法,不像 SED -i 不作出任何形式的复制,而只是去除现有的文件行

I'm trying to remove the first 37 lines from a very, very large file. I started trying sed and awk, but they seem to require copying the data to a new file. I'm looking for a "remove lines in place" method, that unlike sed -i is not making copies of any kind, but rather is just removing lines from the existing file.

下面是我做了什么......

Here's what I've done...

awk 'NR > 37' file.xml > 'f2.xml'
sed -i '1,37d' file.xml

这两个似乎做一个完整的副本。是否有任何其他简单的CLI,能够迅速做到这一点没有一个完整的文档遍历?

Both of these seem to do a full copy. Is there any other simple CLI that can do this quickly without a full document traversal?

推荐答案

有没有简单的方法使用UNIX工具做就地编辑,但这里有一个内置式的文件修改的解决方案,你也许可以修改为你工作(的礼貌罗伯特·博诺米在<一个href=\"https://groups.google.com/forum/#!topic/comp.unix.shell/5PRRZIP0v64\">https://groups.google.com/forum/#!topic/comp.unix.shell/5PRRZIP0v64):

There's no simple way to do inplace editing using UNIX utilities, but here's one inplace file modification solution that you might be able to modify to work for you (courtesy of Robert Bonomi at https://groups.google.com/forum/#!topic/comp.unix.shell/5PRRZIP0v64):

count=`head -37 "$file" |wc -c`
dd if="$file" bs="$count" skip=1 of="$file"

最后的文件应该是 $计数字节,比原来的(因为目标是除去较小的 $计数从一开始字节),所以到结束,我们必须消除最后的 $计数字节。上的GNU系统如Linux这可以通过以下方式实现:

The final file should be $count bytes smaller than the original (since the goal was to remove $count bytes from the beginning), so to finish we must remove the final $count bytes. On a GNU system such as Linux this can be accomplished by:

truncate -s "-$count" "$file"

查看谷歌组线程我的其他建议和信息参考。

See the google groups thread I referenced for other suggestions and info.

这篇关于在UNIX命令行中删除文件的第N行到位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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