我有2个csv文件
文件1包含
product_id, category_id, price
pid01,cat01,10
pid02,cat01,10
pid03,cat01,20
pid04,cat02,30
pid05,cat02,20
pid06,cat03,30
文件2包含
category_id, category_name
cat01,Mouse
cat02,Cat
cat03,Fish
cat04,Dog
我需要这样的结果
product_id, category_id, category_name, price
pid01,cat01,Mouse,10
pid02,cat01,Mouse,10
pid03,cat01,Mouse,20
pid04,cat02,Cat,30
pid05,cat02,Cat,20
pid06,cat03,Fish,30
或
product_id, category_name, price
pid01,Mouse,10
pid02,Mouse,10
pid03,Mouse,20
pid04,Cat,30
pid05,Cat,20
pid06,Fish,30
我如何在Bash或Awk中实现它?
答案 0 :(得分:3)
加入:
join --header -t , -1 2 -2 1 -o 1.1,1.2,2.2,1.3 file1 file2
输出:
pid01,cat01,Mouse,10 pid02,cat01,Mouse,10 pid03,cat01,Mouse,20 pid04,cat02,Cat,30 pid05,cat02,Cat,20 pid06,cat03,Fish,30
答案 1 :(得分:0)
您可以像这样创建一个shell脚本(process_csv.sh):
#!/bin/sh
data=`cat file1.csv | sed -n '/pid/,$ p'`
data2=`cat file2.csv`
echo "product_id, category_id, price, category_name" > final.csv
#since category_id is common in both files, we lookup category names based on that id.
for row in $data
do
cat_id=`printf $row | awk -F "," '{print $2'}`
category_name=`printf "$data2" | grep "$cat_id" | cut -f2 -d','`
#now we write category_name to file and append it to row/line with corresponding product_id
echo $row","$categor_name >> final.csv
done
只需运行“./process_csv.sh”,final.csv文件将包含结果