如何删除文件中的第一行?
我无法搜索特定的字符串,因为它们都非常相似,但我想要一些简单的方法来删除文件中的前 4 行。
它们的长度也都是可变的。我考虑过 Perl,这一切似乎比我想象的要难,但如果可能的话,我想用 Perl、AWK 或 shell 命令来完成。
有人有一个简单的方法可以做到这一点吗?
I can't search for a particular string, since they're all very similar, but I'd like something simple to chop out the first 4 lines in a file.
They're all variable length too. I've had a think about perl, and it all seems harder than I thought, but I'd like to do it in Perl, AWK or a shell command if possible.
Does anybody have a simple way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
将跳过
filename
中的第一行并将其输出到 stdout。-n+2
选项表示从第二行开始输出行。当然,您可以将 2 替换为您需要的任何数字(您的标题和实际问题内容分别为第一和第五)。
Will skip the first line in
filename
and output it to stdout. The-n+2
option means to start outputting lines beginning with the second line.Of course you can substitute 2 with whatever number you need (your title and actual question content say first and fifth, respectively).
sed
是最简单的。要删除文件的第一行,请执行以下操作:或者,要删除前四行,请执行以下操作:
sed
is the simplest. To delete the first line of a file, do:Or, to remove the first four lines, do:
跳过前 4 行
skip first 4 lines
将删除其输入文件的前四行。 (如果您只想删除第一行,则可以使用
'1d'
代替。)-i
标志代表就地编辑,这意味着输入文件也是输出文件,因此更改会写回原始文件。如果您希望文件保持完整并将修改后的内容简单地写入标准输出,只需省略-i
即可。这个和许多其他 sed 1-liners 可以在此处的方便参考中找到:
http://sed.sourceforge.net/sed1line.txt
will delete the first four lines of its input file. (If you only wanted the first line deleted, you could use
'1d'
instead.)The
-i
flag stands for in-place editing, which means that the input file is also the output file, and thus the changes are written back out to the original file. If you'd prefer to have the file left intact and the modified contents simply written to stdout, just omit the-i
.This and many other
sed
1-liners can be found in a handy reference here:http://sed.sourceforge.net/sed1line.txt
等同于
或
等同于
您可以使用与
sed
中相同的-i
。as equivalent to
or
as equivalent to
You can use
-i
same as insed
.一种不需要脚本语言(如 awk、sed、perl 等)的解决方案:
其想法是计算文件中的行数,然后减一,然后将结果传递给“tail”命令。
A solution which requires no scripting language (like awk, sed, perl etc.):
The idea is to count the number of lines in the file, then subtract one, then pass the result to the 'tail' command.
试试这个:
sed -i 1d file
一般规则:
删除
n
第一行:sed '1,nd' file.txt
例如:
sed '1,4d' file.txt
删除前 4 行Try this:
sed -i 1d file
A general rule :
To remove the
n
first lines:sed '1,nd' file.txt
For example:
sed '1,4d' file.txt
to remove the first 4 lines使用文件::领带。这样就很好了。
Use File::Tie. That does just fine.
在文件中搜索第四个“\r\n”或“\n”以及该索引中的子字符串。
Search the file for the fourth "\r\n" or "\n" and substring from that index.