使用 awk,如何从多个文件源获取所需的输出

now*_*wy1 2 awk files merge

我有两个文件:

文件 1:

A,D
B,E
C,F
Run Code Online (Sandbox Code Playgroud)

文件2:

G,H,I
J,K,L
Run Code Online (Sandbox Code Playgroud)

使用awk,我想打印:

A,D
B,E,I
C,F,L
Run Code Online (Sandbox Code Playgroud)

我怎样才能awk实现这一目标?

gle*_*man 5

假设来自 的n行中的额外字段file2应附加到 的最后 n几行file1

awk -F, -v OFS=, 'FNR==NR {a[FNR]=$3; next} {print $0, a[FNR]}' <(tac file2) <(tac file1) | tac
Run Code Online (Sandbox Code Playgroud)
paste -d, <(tac file1) <(cut -d, -f3- <(tac file2)) | tac
Run Code Online (Sandbox Code Playgroud)

这些解决方案在第一行添加一个尾随逗号。您可以通过管道通过上述管道将其删除sed 's/,$//'