如何在 UNIX 上使用行号从文件中提取行?
使用 sed 或类似的方法如何从文件中提取行?如果我想要文件中的第 1、5、1010、20503 行,我该如何获取这 4 行?
如果我需要提取相当多的行怎么办? 如果我有一个包含 100 行的文件,每行代表一个我想从另一个文件中提取的行号,我该怎么做?
Using sed or similar how would you extract lines from a file? If I wanted lines 1, 5, 1010, 20503 from a file, how would I get these 4 lines?
What if I have a fairly large number of lines I need to extract?
If I had a file with 100 lines, each representing a line number that I wanted to extract from another file, how would I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
类似于“sed -n '1p;5p;1010p;20503p'”。执行命令“man sed”了解详细信息。
对于第二个问题,我将输入文件转换为一堆 sed(1) 命令来打印我想要的线条。
Something like "sed -n '1p;5p;1010p;20503p'. Execute the command "man sed" for details.
For your second question, I'd transform the input file into a bunch of sed(1) commands to print the lines I wanted.
使用 awk 就这么简单:
with awk it's as simple as:
@OP,您可以使用 awk 更轻松、更高效地完成此操作。所以对于你的第一个问题
和第二个问题
@OP, you can do this easier and more efficiently with awk. so for your first question
for 2nd question
这不太漂亮,在某些情况下它可能会超出命令长度限制*:
或者它慢得多,但更有吸引力,并且可能表现得更好,兄弟:
一种变体:
您可以加快< code>xarg 版本通过添加
-P
选项和一些大参数(例如 83 或 419 甚至 1177)来实现,但 10 似乎与任何值一样好。*
xargs --show-limits 可能具有指导意义
This ain't pretty and it could exceed command length limits under some circumstances*:
Or its much slower but more attractive, and possibly more well-behaved, sibling:
A variation:
You can speed up the
xarg
versions a little bit by adding the-P
option with some large argument like, say, 83 or maybe 419 or even 1177, but 10 seems as good as any.*
xargs --show-limits </dev/null
can be instructive我会研究 Perl,因为它具有 sed 的正则表达式设施以及围绕它的编程模型,允许您逐行读取文件,计算行数并根据您想要的内容提取(包括来自行号文件)。
I'd investigate Perl, since it has the regexp facilities of
sed
plus the programming model surrounding it to allow you to read a file line by line, count the lines and extract according to what you want (including from a file of line numbers).在 Perl 中:
In Perl: