[英]Linux shell command to copy text data from a file to another
file_1 contents: file_1内容:
aaa 111 222 333 aaa 111222333
bbb 444 555 666 bbb 444555666
ccc 777 888 999 ccc 777888999
file_2 contents: file_2内容:
ddd ddd
eee ee
fff fff
how do i copy only part of the text from file_1 to file_2 我如何只将部分文本从file_1复制到file_2
so that file_2 would become: 这样file_2就会变成:
ddd 111 222 333 ddd 111222333
eee 444 555 666 eee 444555666
fff 777 888 999 fff 777888999
Try with awk: 尝试使用awk:
awk 'NR==FNR{a[FNR]=$2FS$3FS$4;next} {print $0, a[FNR]}' file_1 file_2
Explanation: 说明:
NR
is the current input line, FNR
is the number of input line in current file, you can see that by NR
是当前输入行, FNR
是当前文件中输入行的数量,可以看到
$ awk '{print NR,FNR}' file_1 file_2
1 1
2 2
3 3
4 1
5 2
6 3
So, the condition NR==FNR
is only true when reading the first file, and that's when the columns $2
, $3
, and $4
get saved in a[FNR]
. 因此,条件
NR==FNR
仅在读取第一个文件时为true,即当列$2
, $3
和$4
保存在a[FNR]
。 After reading file_1
, the condition NR==FNR
becomes false and the block {print $0, a[FNR]}
is executed, where $0
is the whole line in file_2
. 读取
file_1
,条件NR==FNR
变为false,并执行块{print $0, a[FNR]}
,其中$0
是file_2
的整行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.