是否可以在第二个字段上加入这两个文件? 我很难从加入中获得任何东西。
join -j2 -t "," file1.csv file2.csv
上面的命令没有输出没有错误。
省略-t“,”sorta有效,但现在这些字段混乱了,尽管排序与我想要的一致。
Academia" "http://www.lackadaisycats.com/comic/1264990398.jpg","Lackadaisy 83,"Lackadaisy
Arithmophobia" "http://www.lackadaisycats.com/comic/1238468042.jpg","Lackadaisy 12,"Lackadaisy
我只是在根据第二列努力让联接工作。我相信空间搞砸了。
我想我可以将第二个字段中的空间转换为_或删除冗余的“Lackadaisy”,但这看起来有点像hackish。
编辑:我删除了第二个字段中的'Lackadaisy'并重新
join -j2 -t "," file1.csv file2.csv
并且仍未获得所需的输出:(
file1.csv
"http://www.lackadaisycats.com/comic/1264990398.jpg" ,"Lackadaisy Academia"
"http://www.lackadaisycats.com/comic/1238468042.jpg" ,"Lackadaisy Arithmophobia"
"http://www.lackadaisycats.com/comic/1292845744.jpg" ,"Lackadaisy Backalley"
"http://www.lackadaisycats.com/comic/1257460213.jpg" ,"Lackadaisy Balderdash"
"http://www.lackadaisycats.com/comic/1213771589.jpg" ,"Lackadaisy Bedlamite"
"http://www.lackadaisycats.com/comic/1265228812.jpg" ,"Lackadaisy Bee-line"
"http://www.lackadaisycats.com/comic/1313510695.jpg" ,"Lackadaisy Begorra"
"http://www.lackadaisycats.com/comic/1168262128.jpg" ,"Lackadaisy Benediction"
"http://www.lackadaisycats.com/comic/1190620435.jpg" ,"Lackadaisy Blindside"
"http://www.lackadaisycats.com/comic/1213855098.jpg" ,"Lackadaisy Blitzwagen"
file2.csv
83 ,"Lackadaisy Academia"
12 ,"Lackadaisy Arithmophobia"
100 ,"Lackadaisy Backalley"
78 ,"Lackadaisy Balderdash"
19 ,"Lackadaisy Bedlamite"
84 ,"Lackadaisy Bee-line"
108 ,"Lackadaisy Begorra"
24 ,"Lackadaisy Benediction"
41 ,"Lackadaisy Blindside"
18 ,"Lackadaisy Blitzwagen"
使用:GNU bash,版本4.3.0(1) - 发行版(i686-pc-linux-gnu)
答案 0 :(得分:2)
file1.csv
中第二个字段之后的空格似乎造成了麻烦。鉴于您将字段分隔符设置为,
,我不知道如何让join忽略这些尾随空格
一种解决方法是将file1.csv
传递给sed以摆脱尾随空格,并通过join将输出提供给bash process substitution。结果命令非常接近原始
join -j2 -t "," <(sed 's/\s\+$//' file1.csv) file2.csv
"Lackadaisy Academia","http://www.lackadaisycats.com/comic/1264990398.jpg" ,83
"Lackadaisy Arithmophobia","http://www.lackadaisycats.com/comic/1238468042.jpg" ,12
"Lackadaisy Backalley","http://www.lackadaisycats.com/comic/1292845744.jpg" ,100
"Lackadaisy Balderdash","http://www.lackadaisycats.com/comic/1257460213.jpg" ,78
"Lackadaisy Bedlamite","http://www.lackadaisycats.com/comic/1213771589.jpg" ,19
"Lackadaisy Bee-line","http://www.lackadaisycats.com/comic/1265228812.jpg" ,84
"Lackadaisy Begorra","http://www.lackadaisycats.com/comic/1313510695.jpg" ,108
"Lackadaisy Benediction","http://www.lackadaisycats.com/comic/1168262128.jpg" ,24
"Lackadaisy Blindside","http://www.lackadaisycats.com/comic/1190620435.jpg" ,41
"Lackadaisy Blitzwagen","http://www.lackadaisycats.com/comic/1213855098.jpg" ,18
答案 1 :(得分:1)
另一种选择是使用awk
(如果您有尾随空格,可以使用sub
的{{1}}函数删除它们
awk
$ awk 'BEGIN{FS=OFS=" ,"}NR==FNR{a[$2]=$1;next}{sub(/ *$/,"",$NF);$NF=$NF OFS a[$NF]}1' file2 file1
"http://www.lackadaisycats.com/comic/1264990398.jpg" ,"Lackadaisy Academia" ,83
"http://www.lackadaisycats.com/comic/1238468042.jpg" ,"Lackadaisy Arithmophobia" ,12
"http://www.lackadaisycats.com/comic/1292845744.jpg" ,"Lackadaisy Backalley" ,100
"http://www.lackadaisycats.com/comic/1257460213.jpg" ,"Lackadaisy Balderdash" ,78
"http://www.lackadaisycats.com/comic/1213771589.jpg" ,"Lackadaisy Bedlamite" ,19
"http://www.lackadaisycats.com/comic/1265228812.jpg" ,"Lackadaisy Bee-line" ,84
"http://www.lackadaisycats.com/comic/1313510695.jpg" ,"Lackadaisy Begorra" ,108
"http://www.lackadaisycats.com/comic/1168262128.jpg" ,"Lackadaisy Benediction" ,24
"http://www.lackadaisycats.com/comic/1190620435.jpg" ,"Lackadaisy Blindside" ,41
"http://www.lackadaisycats.com/comic/1213855098.jpg" ,"Lackadaisy Blitzwagen" ,18
函数删除尾随空格。