Linux shell 命令将文本数据从一个文件复制到另一个文件

Linux shell command to copy text data from a file to another

file_1内容:
aaa 111 222 333
bbb 444 555 666
ccc 777 888 999

file_2内容:
ddd
eee
fff

如何只将部分文本从 file_1 复制到 file_2
这样 file_2 就会变成:

ddd 111 222 333
eee 444 555 666
fff 777 888 999

用 awk 试试:

awk 'NR==FNR{a[FNR]=FSFS;next} {print [=10=], a[FNR]}' file_1 file_2

解释:

NR为当前输入行,FNR为当前文件输入行数,通过

可以看出
$ awk '{print NR,FNR}' file_1 file_2
1 1
2 2
3 3
4 1
5 2
6 3

因此,条件 NR==FNR 仅在读取第一个文件时为真,即 </code>、<code></code> 列保存在 <code>a[FNR]。读取file_1后,条件NR==FNR变为假,块{print [=21=], a[FNR]}被执行,其中[=22=]file_2.

中的整行