3

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

4 Answers 4

15
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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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 >:[
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).
6
tr ',' '\n' < inputfile > outputfile

For left-to-right dog people:

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

Comments

2

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

Comments

1

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!

1 Comment

Use gunzip -c instead of unzip. curl ... | gunzip -c | sed ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.