将csv转换为文本

将csv转换为文本

问题描述:

我有一个csv(大)的ip地址文件,并希望在bash中转换成单行ip地址。

将csv转换为文本

aa.bb.cc.dd,aa.bb.cc.dd,aa.bb.cc.dd ..


aa.bb.cc.dd
AA。 bb.cc.dd
aa.bb.cc.dd
[..]

在问题IP列表,

http://www.stopforumspam.com/downloads/bannedips.zip

cat file | tr ',' '\n' > fixed.txt 

tr做简单的字符翻译(和更多,但多数民众赞成它在这里做什么)。这只是将所有逗号转换为换行符。

+1

我没有做到这一点,但它可能是因为无偿使用猫。由于tr本身可以输入一个输入文件,因此不需要将cat转换为tr。 是否有任何特殊的原因,你选择这样做? – 2010-06-15 04:58:20

+0

想说 - >“tr本身可以通过STDIN将文件内容作为参数” - 耗尽时间,并且无法编辑原始评论>:[ – 2010-06-15 05:10:03

+0

我知道猫是不必要的,它会产生额外的进程,但在这种情况下(并且在很多情况下)它更具可读性(数据从左到右)。 – luke 2010-06-15 05:11:09

tr ',' '\n' <inputfile> outputfile 

左到右的狗的人:

< inputfile tr ',' '\n' > outputfile 

假设该文件是不是你的服务器上,这将做一个在线为您的所有工作:

curl http://www.stopforumspam.com/downloads/bannedips.zip | gunzip -c | sed s/,/\\n/g > bannedips.txt 

You can't use unzip for this,如果你想通过管道飞行。

感谢您的建议丹尼斯!

+1

使用'gunzip -c'而不是'unzip'。卷曲... | gunzip -c | sed ...' – 2010-06-15 00:40:03

在bash,你可以使用while循环:

while read -d, ip; 
    do echo $ip; 
done <file.csv >output 

在awk中,你可以用更少的时间获得相同的结果:

awk -v RS=, '$1' file.csv >output