user349418
user349418

Reputation: 2217

converting a csv into text

I have a csv (large) file of ip addresses, and wish to 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

Upvotes: 3

Views: 18946

Answers (4)

Dennis Williamson
Dennis Williamson

Reputation: 359965

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

For left-to-right dog people:

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

Upvotes: 6

Peter Ajtai
Peter Ajtai

Reputation: 57685

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!

Upvotes: 1

marco
marco

Reputation: 4665

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

Upvotes: 2

luke
luke

Reputation: 14788

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.

Upvotes: 15

Related Questions