Prev: How to determine integer?
Next: read multiple fields separated by : and fields separated by , in loop stored in $var
From: Chimu on 13 Mar 2010 08:15 I got data in $x separated by :, further each field has 4 values and i want get those. So i come with IFS and read code Input x="tab1,c,1,3,db1:tab2,d,21,13,db45:tab12,t,11,16,db4" output Data feed: tab1 c 1 3 db1 Data feed: tab2 d 21 13 db45 Data feed: ab12 t 11 16 db4 my code so far ~~~~~~~~~~ while IFS=: read -r field do while IFS=, read -r a b c d do echo "Data feed: $a $b $c $d" done <<<"$field" #IFS=: done <<<$x ~~~~~~~~~~ so far it is not working and i'm not able to pull data. i don't wanna use external tool just bash builtin and IFS. any thoughts on how to fix this? are you allowed to use multiple IFS? btw, I'm using bash 3.x on Debian Linux server.
From: pk on 15 Mar 2010 06:46 Chimu wrote: > I got data in $x separated by :, further each field has 4 values and i > want get those. So i come with IFS and read code > Input > x="tab1,c,1,3,db1:tab2,d,21,13,db45:tab12,t,11,16,db4" > > output > Data feed: tab1 c 1 3 db1 > Data feed: tab2 d 21 13 db45 > Data feed: ab12 t 11 16 db4 > > my code so far > ~~~~~~~~~~ > while IFS=: read -r field > do > while IFS=, read -r a b c d > do > echo "Data feed: $a $b $c $d" > done <<<"$field" > #IFS=: > done <<<$x > ~~~~~~~~~~ > > so far it is not working and i'm not able to pull data. i don't wanna > use external tool just bash builtin and IFS. any thoughts on how to > fix this? are you allowed to use multiple IFS? btw, I'm using bash 3.x > on Debian Linux server. You can try oifs=$IFS; IFS=\: set -- "$x" IFS=$oifs for f; do IFS=, read a b c d e <<<"$f" echo "Data feed: $a $b $c $d $e" done
From: pk on 15 Mar 2010 06:48
pk wrote: > oifs=$IFS; IFS=\: > set -- "$x" Sorry, wrong. The should be set -- $x |