Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a comma separated strings in Bash

Tags:

bash

I have this file with 20k+ IPs inside:

104.20.15.220,104.20.61.219,104.20.62.219,104.20.73.221,104.20.74.221,104.20.14.220
104.20.15.220,104.20.73.221,104.20.74.221,104.25.195.107,104.25.196.107,104.20.14.220
91.215.154.209
...

The question is how to split in into single IPs on each string:

104.20.15.220
104.20.61.219
like image 784
Vasiliy Avatar asked Sep 16 '16 09:09

Vasiliy


People also ask

How do I split a string on a delimiter in bash?

In bash, a string can also be divided without using $IFS variable. The 'readarray' command with -d option is used to split the string data. The -d option is applied to define the separator character in the command like $IFS. Moreover, the bash loop is used to print the string in split form.

How split a string in bash Linux?

To split a string in bash shell by a symbol or any other character, set the symbol or specific character to IFS and read the string to a variable with the options -ra mentioned in the below example. Run the above bash shell script in terminal. The default value of IFS is single space ' ' .

How do you separate Comma Separated Values in Linux?

Let's say you have a long string with several words separated by a comma or underscore. You want to split this string and extract the individual words. You can split strings in bash using the Internal Field Separator (IFS) and read command or you can use the tr command.

How do you split a line in a word in shell script?

The -a option of read will allow you to split a line read in by the characters contained in $IFS . #!/bin/bash filename=$1 while read LINE do echo $LINE | read -a done < $filename should it work?


2 Answers

Just replace a comma with a new line with either of these commands:

tr ',' '\n' < file

sed 's/,/\n/g' file

perl 's/,/\n/g' file

awk 'gsub(/,/,"\n")' file

... or match every block of text up to a comma or the end of line:

grep -oP '.*?(?=,|$)' file

... or loop through the fields and print them:

awk -F, '{for(i=1;i<=NF;i++) print $i}' file

... or set the record separator to the comma and let awk do all the work:

awk -v RS=, '1' file
awk 1 RS=, file

... or match the IPs, you can use the regex from Matching IPv4 Addresses:

grep -oE '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' file

They all return:

104.20.15.220
104.20.61.219
104.20.62.219
104.20.73.221
...
like image 153
fedorqui 'SO stop harming' Avatar answered Oct 14 '22 01:10

fedorqui 'SO stop harming'


This will transform all the commands into newline.

tr ',' '\n' <filename

or

awk 'BEGIN{FS=",";OFS="\n"}{$1=$1}1' filename
like image 34
P.... Avatar answered Oct 14 '22 03:10

P....