MOHAMED MOHAMED - 1 month ago 10
Linux Question

How to redirect a line fields to vars?

I have the following output from a command:

1 192.168.1.1 (192.168.1.1) 3.032 ms
2 172.26.0.15 (41.226.22.21) 53.825 ms
3 172.26.0.13 (172.26.0.13) 57.146 ms
4 193.95.19.137 (193.95.19.137) 60.792 ms
5 193.95.96.69 (193.95.96.69) 65.873 ms


I want to redirect the fields of each line to the variables
nbr
,
host
,
ip
and
time


I read all lines with:

while read LINE; do
echo $LINE #How to redirect the line fields to vars
done < <(traceroute yahoo.fr)

Answer

Ex:

while read _ ip ip2 ms _; do
    echo "$ip $ms ms"
done < <(traceroute yahoo.fr)

Where _ is a placeholder variable.

To strip out the parentheses like in your comment :

% x='(127.0.0.1)'
% echo ${x//[)(]/}                                        
127.0.0.1

this is bash parameter expansion