我有两个文件; file1.txt和file2.txt。并希望像这样合并它们:
name1 note1
name2 note2
name3 note3
file1.txt
name2 note2_2
name3 note2_3
name1 note2_1
file2.txt
name1 note1 note2_1
name2 note2 note2_2
name3 note3 note2_3
result.txt
我做了一些代码,但是我不确定如何附加它。
#!/bin/bash
cut -d' ' -f1 file1.txt > names1.txt
while read -a rows
do
for i in "${rows[@]}"
do
grep "$i" | cut -f 2- -d ' ' file2.txt > new.txt
done
*here I should append it (I think)*
done < names1.txt
我知道sed可用于附加类似的内容;sed 's/$/ *the first word of each line from new.txt*/' file1.txt
或使用paste file1.txt new.txt > fileresults.txt
awk '{print $2}' file2.txt | xargs -I {} awk '$0=$0" {}"' file1.txt
[awk '{print $2}' file2
读取file2
的第二列。
| xargs -I {}
将第一个awk
的标准输出插入到出现{}
的下一个点。您还可以选择其他东西作为占位符。awk '$0=$0" {}"' file1
读取file1
的所有列,并将xargs
参数放在空白后面。 join(1)
它确实要求文件在连接字段上排序(默认为第一列),因此,如果您的真实文件1未排序,则与上述示例中的文件2排序相同。