简体   繁体   English

合并来自不同文件的列

[英]Combine columns from different files

I have two text files that have these structures: 我有两个具有这些结构的文本文件:

File 1 档案1

Column1:Column2
Column1:Column2
...

File 2 档案2

Column3
Column3
...

I would like to create a file that has this file structure: 我想创建一个具有此文件结构的文件:

Column1:Column3
Column1:Column3
...

Open to any suggestions, but it would be nice if the solution can be done from a Bash shell, or sed / awk / perl / etc ... 打开任何建议,但如果解决方案可以从Bash shell或sed / awk / perl / etc完成,那将是很好的...

cut -d: -f1 "File 1" | paste -d: - "File 2"

这会从文件1中删除字段1(由冒号分隔)并将其粘贴到文件2中的唯一列,用冒号分隔输出字段。

Here's an awk solution. 这是一个awk解决方案。 It assumes file1 and file2 have an equal number of lines. 它假定file1和file2具有相同的行数。

awk -F : '{ printf "%s:",$1; getline < "file2"; print }' < file1

Since a pure bash implementation hasn't been suggested, also assuming an equal number of lines (bash v4 only): 由于尚未提出纯bash实现,也假设行数相等(仅限bash v4):

mapfile -t file2 < file2

index=0
while IFS=: read -r column1 _; do
        echo "$column1:${file2[index]}"
        ((index++))
done < file1

bash v3: bash v3:

IFS=$'\n' read -r -d '' file2 < file2

index=0
while IFS=: read -r column1 _; do
        echo "$column1:${file2[index]}"
        ((index++))
done < file1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM