Unix 使用包含行号的输入文件从文件打印行
我有一个文件Unix 使用包含行号的输入文件从文件打印行,unix,awk,grep,Unix,Awk,Grep,我有一个文件numbers.txt,每行都包含一个数字 我想使用该文件打印文件lines.txt中的行,即与numbers.txt中的数字对应的行 这可以通过awk完成,还是直接在Unix命令行中通过其他方式完成 例如: numbers.txt: 1 2 4 6 1: line one 2: line two 3: line three ... 6: line six cat lines.txt | head -<n> | tail -1 lines.txt: 1 2 4 6
numbers.txt
,每行都包含一个数字
我想使用该文件打印文件lines.txt
中的行,即与numbers.txt
中的数字对应的行
这可以通过awk
完成,还是直接在Unix命令行中通过其他方式完成
例如:
numbers.txt
:
1
2
4
6
1: line one
2: line two
3: line three
...
6: line six
cat lines.txt | head -<n> | tail -1
lines.txt
:
1
2
4
6
1: line one
2: line two
3: line three
...
6: line six
cat lines.txt | head -<n> | tail -1
输出:
1: line one
2: line two
4: line four
6: line six
试试这个awk one liner:
awk 'NR==FNR{n[$0];next}FNR in n' numbers.txt lines.txt
试试这个awk one liner:
awk 'NR==FNR{n[$0];next}FNR in n' numbers.txt lines.txt
您可以使用此命令在文件
lines.txt
中输出行n
:
1
2
4
6
1: line one
2: line two
3: line three
...
6: line six
cat lines.txt | head -<n> | tail -1
注意:如果
lines.txt
中没有与数字对应的行,它将输出文件中的最后一行;您需要一个额外的测试来检查并避免这种情况。您可以使用此命令在文件lines.txt
中输出行n
:
1
2
4
6
1: line one
2: line two
3: line three
...
6: line six
cat lines.txt | head -<n> | tail -1
parallel -ka numbers.txt 'sed -n {}p lines.txt'
注意:如果lines.txt
中没有与数字对应的行,它将输出文件中的最后一行;您需要额外的测试来检查并避免这种情况
parallel -ka numbers.txt 'sed -n {}p lines.txt'
有更多关于并行的信息吗
是关于并行的更多信息我从来没有尝试过任何这样的事情,但是读这篇文章我从来没有尝试过任何这样的事情,但是读这篇文章而不是
=
,不必要的paren和不必要的{print}
,=
而不是=
,不必要的paren和不必要的{print}
。