awk 合并部分匹配的文件并附加

awk 合并部分匹配的文件并附加

在下面,如果的位于的行中的某处,awk我尝试cppaste的中每一行匹配f2到的$3中。在和中总会有一个匹配项(通常大于 1),并且我的实际数据要大得多(几百行)。将的行粘贴到的中时,的值将以 附加在行尾。值也是来自。其中大部分是静态文本,但来自的值在第三个之后。以 开头的行按原样打印。确实执行了,但输出没有变化。谢谢 :)。f1$2f1f2f1f2f2$3f1$1/test/id/$1_raw.file_fixed.txt$1f1$1/R_2019awk

我还尝试了另一种,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

相关内容