Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 使用包含行号的输入文件从文件打印行_Unix_Awk_Grep - Fatal编程技术网

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}