如何在 UNIX 上使用行号从文件中提取行?

发布于 2024-08-17 00:41:51 字数 141 浏览 5 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

忆离笙 2024-08-24 00:41:51

类似于“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.

月野兔 2024-08-24 00:41:51

使用 awk 就这么简单:

awk 'NR==1 || NR==5 || NR==1010' "file"

with awk it's as simple as:

awk 'NR==1 || NR==5 || NR==1010' "file"
雪落纷纷 2024-08-24 00:41:51

@OP,您可以使用 awk 更轻松、更高效地完成此操作。所以对于你的第一个问题

awk 'NR~/^(1|2|5|1010)$/{print}' file

和第二个问题

awk 'FNR==NR{a[$1];next}(FNR in a){print}' file_with_linenr file

@OP, you can do this easier and more efficiently with awk. so for your first question

awk 'NR~/^(1|2|5|1010)$/{print}' file

for 2nd question

awk 'FNR==NR{a[$1];next}(FNR in a){print}' file_with_linenr file
離殇 2024-08-24 00:41:51

这不太漂亮,在某些情况下它可能会超出命令长度限制*

sed -n "$(while read a; do echo "${a}p;"; done < line_num_file)" data_file

或者它慢得多,但更有吸引力,并且可能表现得更好,兄弟:

while read a; do echo "${a}p;"; done < line_num_file | xargs -I{} sed -n \{\} data_file

一种变体:

xargs -a line_num_file -I{} sed -n \{\}p\; data_file

您可以加快< code>xarg 版本通过添加 -P 选项和一些大参数(例如 83 或 419 甚至 1177)来实现,但 10 似乎与任何值一样好。

*xargs --show-limits 可能具有指导意义

This ain't pretty and it could exceed command length limits under some circumstances*:

sed -n "$(while read a; do echo "${a}p;"; done < line_num_file)" data_file

Or its much slower but more attractive, and possibly more well-behaved, sibling:

while read a; do echo "${a}p;"; done < line_num_file | xargs -I{} sed -n \{\} data_file

A variation:

xargs -a line_num_file -I{} sed -n \{\}p\; data_file

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

枫以 2024-08-24 00:41:51

我会研究 Perl,因为它具有 sed 的正则表达式设施以及围绕它的编程模型,允许您逐行读取文件,计算行数并根据您想要的内容提取(包括来自行号文件)。

my $row = 1
while (<STDIN>) {
   # capture the line in $_ and check $row against a suitable list.
   $row++;
}

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).

my $row = 1
while (<STDIN>) {
   # capture the line in $_ and check $row against a suitable list.
   $row++;
}
伴梦长久 2024-08-24 00:41:51

在 Perl 中:

perl -ne 'print if $. =~ m/^(1|5|1010|20503)$/' file

In Perl:

perl -ne 'print if $. =~ m/^(1|5|1010|20503)$/' file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文