Hi there,
I'm using the result of an sql query in a script:
santiago@videos:~$ mysql ndtv -e 'SELECT id, ip FROM terminal' | sed '1d'
3 172.16.100.162
4 172.16.121.151
5 172.16.121.152
6 172.16.146.151
7 172.16.146.152
14 172.16.150.151
17 172.16.121.154
19 172.16.121.153
But I'd like to separate each column. And the following doesn't work:
santiagoo@videos:~$ cat myscript
mysql ndtv -e 'SELECT id, ip FROM terminal' | sed '1d' | while IFS= read -r line; do
echo "Terminal ${line%%\t*} (${line##*\t})"
done
santiago@videos:~$ ./mycript
Terminal 3 172.16.100.162 (3 172.16.100.162)
Terminal 4 172.16.121.151 (4 172.16.121.151)
Terminal 5 172.16.121.152 (5 172.16.121.152)
Terminal 6 172.16.146.151 (6 172.16.146.151)
Terminal 7 172.16.146.152 (7 172.16.146.152)
Terminal 14 172.16.150.151 (14 172.16.150.151)
Terminal 17 172.16.121.154 (17 172.16.121.154)
Terminal 19 172.16.121.153 (19 172.16.121.153)
What I expected was:
Terminal 3 (172.16.100.162)
Terminal 4 (172.16.121.151)
Terminal 5 (172.16.121.152)
Terminal 6 (172.16.146.151)
Terminal 7 (172.16.146.152)
Terminal 14 (172.16.150.151)
Terminal 17 (172.16.121.154)
Terminal 19 (172.16.121.153)
Why can't I use tabs in string manipulations?
Since ${line%%\t*} doesn't work, what's the correct syntax?
Thanks for your help
Santiago
#!/bin/ksh
ndtv -e 'SELECT id, ip FROM terminal' | sed '1d' | while read -r f1 f2
do
printf "Terminal %s\t(%s)\n" "${f1}" "${f2}"
done
Thanks vgersh99 for your help.
Unfortunately, this doesn't work with bash and I'm not allowed to install other shells.
But I came up with this new solution:
patricio@videos:~$ cat myscript
for record in $(mysql ndtv -e 'SELECT CONCAT(id, "-", ip) FROM terminal ORDER BY ip' | sed '1d'); do
ip=${record##*-}
id=${record%%-*}
echo -en "================================================================================\15"
echo -n "Terminal $id ($ip) "
if ping -c1 -w1 $ip > /dev/null 2>&1; then
echo
ssh root@$ip "$1";
else
echo 'UNREACHABLE '
fi
done
patricio@videos:~$ ./myscript 'hostname'
Terminal 3 (172.16.100.162) ====================================================
terminal3
Terminal 4 (172.16.121.151) ====================================================
terminal4
Terminal 5 (172.16.121.152) ====================================================
terminal5
Terminal 19 (172.16.121.153) ===================================================
terminal18
Terminal 17 (172.16.121.154) UNREACHABLE =======================================
Terminal 6 (172.16.146.151) ====================================================
terminal6
Terminal 7 (172.16.146.152) ====================================================
terminal7
Terminal 14 (172.16.150.151) UNREACHABLE =======================================
patricio@videos:~$
Fot some reason that I don't understand, if I keep using while, only the first line is parsed:
patricio@videos:~$ cat myscript
mysql -u ndtv -pndtv ndtv -e 'SELECT CONCAT(id, "-", ip) FROM terminal ORDER BY ip' | sed '1d' | while IFS= read -r record; do
ip=${record##*-}
id=${record%%-*}
echo -en "================================================================================\15"
echo -n "Terminal $id ($ip) "
if ping -c1 -w1 $ip > /dev/null 2>&1; then
echo
ssh root@$ip "$1";
else
echo 'UNREACHABLE '
fi
done
patricio@videos:~$ ./myscript 'hostname'
Terminal 3 (172.16.100.162) ====================================================
terminal3
patricio@videos:~$
Can you identify the problem?
Santiago