BSP*_*BSP 2 awk text-processing join
我想合并两个表:
文件 1:
1 今天
2 明天
3 红色
文件2:
1 很多
1 有时
2 在工作
2 在家里
2 有时
3 新
所需的输出(文件 3):
1 今天很多
1 今天有时
2 明天上班
2 明天在家
2 明天有时
3 红色 新
我想出了以下内容:
awk -F '[\t]' -v OFS='\t' '{i=$1;$1=x} NR==FNR{A[i]=$0;next} A[i]{print i,$0A[i]}' file2 file1 > file3
Run Code Online (Sandbox Code Playgroud)
但是,它只给了我:
1 今天有时
2 明天有时
3 红色 新
尝试:
$ awk 'FNR==NR{a[$1]=$2;next};{$1 = $1"\t"a[$1]}1' OFS='\t' file1 file2
1 today a lot
1 today sometimes
2 tomorrow at work
2 tomorrow at home
2 tomorrow sometimes
3 red new
Run Code Online (Sandbox Code Playgroud)