tags:

views:

95

answers:

4

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

+11  A: 
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.

luke
why the downvote?
luke
I didn't do it, but it may be because of the gratuitous cat use. There's no need to pipe cat to tr, since tr itself can take an input file. Is there any special reason you chose to do it that way?
Peter Ajtai
Meant to say ---> "tr itself can take the contents of a file as an argument via STDIN" -- ran out of time, and can't edit orginal comment >:[
Peter Ajtai
i know the cat is unnecessary and it spawns an extra process, but in this case (and in many cases) it is more readable (the data flows left to right).
luke
It's those dang dog people.
Peter Ajtai
+5  A: 
tr ',' '\n' < inputfile > outputfile

For left-to-right dog people:

< inputfile tr ',' '\n' > outputfile
Dennis Williamson
+1  A: 

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!

Peter Ajtai
Use `gunzip -c` instead of `unzip`. `curl ... | gunzip -c | sed ...`
Dennis Williamson
+1  A: 

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
marco