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
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.
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 ' ' .
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.
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?
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
...
This will transform all the commands into newline.
tr ',' '\n' <filename
or
awk 'BEGIN{FS=",";OFS="\n"}{$1=$1}1' filename
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With