开发者

converting a csv into text

I have a csv (large) file of ip addresses, and wish t开发者_如何学Co covert into single line ip address in bash.

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

into

aa.bb.cc.dd

aa.bb.cc.dd

aa.bb.cc.dd

[..]

The list of ips in question,

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


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

tr does simple character translation (and much more but thats what its doing here). this just translates all the commas to newlines.


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

For left-to-right dog people:

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


In bash you can use a while loop:

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

In awk, you can get the same result with less time:

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


Assuming that file is not on your server, this will do all the work for you in one line:

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

You can't use unzip for this, if you want it flying through the pipes.

Thanks for the suggestion Dennis!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜