CSV:提取第二列并保存为包含第一列文件名的新文件

时间:2014-01-24 01:49:25

标签: bash unix csv sed awk

我有一个 CSV文件master.csv

AA,"Today is hot"
AA,"Tomorrow will rain"
BB,"Snowing on Tuesday"
CC,"Thunderstorm last week"
CC,"Yesterday was sunny"

我想提取第二列内容并将它们保存到一个新文件中,其中column1作为文件名。

期望的输出:

AA.txt
Today is hot
Tomorrow will rain

BB.txt
Snowing on Tuesday

CC.txt
Thunderstorm last week
Yesterday was sunny

是否有方便的awk或sed one-liner可以做到这一点?我现在正在bash中尝试一个while循环,但它变得有点笨拙。感谢您的任何意见!

5 个答案:

答案 0 :(得分:2)

while IFS=, read -r filename string; do
    string=${string#\"}
    echo "${string%\"}" >> "$filename.txt"
done <<END
AA,"Today is hot"
AA,"Tomorrow will rain"
BB,"a string \"with, inner\" quotes"
BB,"Snowing on Tuesday"
CC,"Thunderstorm last week"
CC,"Yesterday was sunny"
END

more {AA,BB,CC}.txt
::::::::::::::
AA.txt
::::::::::::::
Today is hot
Tomorrow will rain
::::::::::::::
BB.txt
::::::::::::::
a string \"with, inner\" quotes
Snowing on Tuesday
::::::::::::::
CC.txt
::::::::::::::
Thunderstorm last week
Yesterday was sunny

答案 1 :(得分:1)

使用awk

awk -F "[,\"]" '{print $3 > $1 ".txt"}' file

答案 2 :(得分:1)

perl -MText::CSV_XS -e '
    $csv=Text::CSV_XS->new();
    while (my $row=$csv->getline(ARGV)) {
        system qq(echo "$$row[1]" >>"$$row[0].txt")
    }
' master.csv

或更安全

perl -MText::CSV_XS -E '
    $csv=Text::CSV_XS->new();
    while (my $row=$csv->getline(ARGV)) {
        open my$f,">>",$row->[0].".txt" or die $!;
        say $f $row->[1];
        close $f;
    }
' master.csv

答案 3 :(得分:0)

我的awk版本,以防引号字符串

中有逗号
awk -F "\"" '{print $2 > gensub(/,/, "", "g",$1)".txt"}' master.csv

使用"作为分隔符
移除,中的所有$1,并将$2的内容发送到.txt文件

答案 4 :(得分:0)

这可能适合你(GNU sed):

sed -nr 's/([^,]*),(.*)/echo \2 >>\1.txt/e' file