How do I real from a file and split into variables in BASH?

I have a file with lines in the following format:
10.15.0.1; 10.15.1.27; 10.15.5.50; 142.5.6.188, 142.9.88.122

There is always going to be 3 IP addresses separated by semi-colons, then one or more IP addresses separated by commas.
I need to process the file and run several commands using the first three IP addresses, then run a several commands on each of the comma-separated IPs.

I tried using IFS and nested for loops but didn't know how to pull off the second loop.
jasonzxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wesly_chenCommented:
IP1=awk -F";" '{print $1}' <path-to-file>
IP2=awk -F";" '{print $2}' <path-to-file>
IP3=awk -F";" '{print $3}' <path-to-file>
0
chenry334Commented:
You can use awk to parse the string... just use multiple field separators - ie:

input:
chenry@home:~$ echo "10.15.0.1; 10.15.1.27; 10.15.5.50; 142.5.6.188, 142.9.88.122" | awk 'BEGIN { FS = "[;,]"} ; {print "/path/to/command "$1";/path/to/command "$2";/path/to/command "$3";/path/to/a/different/command"$4}'

output:
/path/to/command 10.15.0.1;/path/to/command  10.15.1.27;/path/to/command  10.15.5.50;/path/to/a/different/command 142.5.6.188
0
farzanjCommented:
Try this:
#!/bin/bash

FILE='file.txt'
cat $FILE | while read line
do
IP1=$(awk -F';' '{print $1}')
IP2=$(awk -F';' '{print $2}')
IP3=$(awk -F';' '{print $3}')
IP4=$(awk -F';' '{print $4}' | awk -F',' '{print $1}')
IP5=$(awk -F';' '{print $4}' | awk -F',' '{print $2}')
echo $IP1

#NOW DO YOUR PROCESSING
done

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

farzanjCommented:
If you are open to use perl you can do something like this
#!/usr/bin/perl


my $File = 'filename';

open (IN, $FILE) or die "Could not open the file $!";
my @lines = <IN>;
close IN;

foreach my $line (@lines)
{
   $line =~ m/([\d.]+); ([\d.]+); ([\d.]+); ([\d.]+), ([\d.]+)/;
   #These lines are just to show you where the values are
   #You can start using them now
   my $IP1 = $1;
   my $IP2 = $2;
   my $IP3 = $3;
   my $IP4 = $4;
   my $IP5 = $5;
}

Open in new window

0
TintinCommented:
i think this is the easiest approach
#!/bin/bash
sed "s/[;,]//g" file|while read ip1 ip2 ip3 ips
do
  echo "ip1=$ip1"
  echo "ip2=$ip2"
  echo "ip3=$ip3"

  for ip in $ips
  do
    echo "IP=$ip"
  done
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jasonzxAuthor Commented:
Tintin: That works great, it's easy to follow and solves my issue of an unknown amount of IPs at the end.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.