![awk 合并部分匹配的文件并附加](https://linux22.com/image/1588471/awk%20%E5%90%88%E5%B9%B6%E9%83%A8%E5%88%86%E5%8C%B9%E9%85%8D%E7%9A%84%E6%96%87%E4%BB%B6%E5%B9%B6%E9%99%84%E5%8A%A0.png)
在下面,如果的位于的行中的某处,awk
我尝试cp
将paste
的中每一行匹配f2
到的$3
中。在和中总会有一个匹配项(通常大于 1),并且我的实际数据要大得多(几百行)。将的行粘贴到的中时,的值将以 附加在行尾。值也是来自。其中大部分是静态文本,但来自的值在第三个之后。以 开头的行按原样打印。确实执行了,但输出没有变化。谢谢 :)。f1
$2
f1
f2
f1
f2
f2
$3
f1
$1
/test/id/$1_raw.file_fixed.txt
$1
f1
$1
/
R_2019
awk
我还尝试了另一种,awk
但没有成功。
f1
xyxy_0268 0000-yyyy
xyxy_0270 1111-xxxx
R_0000_00_02_00_45_32_xxxx_x0-0000-100-x0.0_xxxx_xxxx_xxxx_xxxx_xxxx_xxxx
f2
/path/to/the/xxx/data/0000-yyyy_v1_0000-yyyy_RNA_v1/190326-Control_v1_20190328071906449
/path/to/the/xxx/data/00-0000_xxxx-03_v1/00-0000_xxxx-03_v1_20190322115521953
/path/to/the/xxx/data/1111-xxxx-03_v1/1111-xxxx-03_v1_20190322115521953
期望
xyxy_0268 0000-yyyy /path/to/the/xxx/data/0000-yyyy_v1_0000-yyyy_RNA_v1/190326-Control_v1_20190328071906449/test/id/xyxy_0268_raw.file_fixed.txt
xyxy_0270 1111-xxxx /path/to/the/xxx/data/1111-xxxx-03_v1/1111-xxxx-03_v1_20190322115521953/test/id/xyxy_0270_raw.file_fixed.txt
R_0000_00_02_00_45_32_xxxx_x0-0000-100-x0.0_xxxx_xxxx_xxxx_xxxx_xxxx_xxxx
awk
awk 'NR==FNR {for(i=1; i<=NF; i++) id[$i]=$1; next} $2 in id{$3=id[$i] "/test/id/" $1 "_raw.file_fixed.txt"}1' f2 f1
awk 'NR==FNR {for(i=1; i<=NF; i++) id[$i]=$1; next} $2 in id{$3=id/$2/ "/test/id/" $1 "_raw.file_fixed.txt"}1' f2 f1
答案1
$ awk 'NR==FNR {a[$0]++; next} {for (i in a) if (i~$2 && $2 != "") {print $0" /test/id/"$2"_raw.file_fixed.txt"; next}} {print}' f2 f1
xyxy_0268 0000-yyyy /test/id/0000-yyyy_raw.file_fixed.txt
xyxy_0270 1111-xxxx /test/id/1111-xxxx_raw.file_fixed.txt
R_0000_00_02_00_45_32_xxxx_x0-0000-100-x0.0_xxxx_xxxx_xxxx_xxxx_xxxx_xxxx