如何仅对文件的第一行进行更改? [英] How can I make changes to only the first line of a file?

查看:73
本文介绍了如何仅对文件的第一行进行更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我可以在sed中使用哪种模式在大文件(〜2 GB)的第一行中进行更改.首选使用sed只是因为我认为它必须比Python或Perl脚本快.

I would like to know which pattern can I use in sed to make changes in the first line of huge files (~2 GB). The preference for sed is only because I assume it must be faster than a Python or Perl script.

文件具有以下结构:

field 1, field 2, ... field n
data

,考虑到每个字段的标识符中都有空格的可能性,我需要用下划线替换每个空格:

and, given the likelihood of having spaces in the identifier for every field, I need to replace every space by an underscore in this way:

**BEFORE** 
the first name,the second name,the first surname,a nickname, ...
data

**AFTER**
the_first_name,the_second_name,the_first_surname,a_nickname, ...
data

任何指向要使用的正确模式的指针,或者其他脚本解决方案都很好.

Any pointers to the right pattern to use, or another scripting solution would be great.

推荐答案

编辑前10行

sed -i -e '1,10s/ /_/g'

在Perl中,您可以在标量上下文中使用触发器运算符:

In Perl, you can use the flip-flop operator in scalar context:

perl -i -pe 's/ /_/g if 1 .. 10'

这篇关于如何仅对文件的第一行进行更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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