Delimiters in Bash
Posted by sighK on May 29th, 2009
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
cat /etc/passwd|while read line; do
IFS=”:”
read username pass auid agid agroup ahome ashell<<EOF
$line
EOF
echo $username
echo $pass
echo $auid
echo $agid
echo $agroup
echo $ahome
echo $ashell
echo “--------”
done
Simplified version
IFS=”:”
read first_name last_name< <(echo “Jeff:Price”)
you could use this method on a never ending stream of data
Thats all for now.


June 17th, 2009 at 4:46 pm
Hmm, not working for me. Outputs just a bunch of blank lines separated by the “——–” lines.
I added #!/bin/bash at the top just to be sure and if I echo $line, it prints the lines from /etc/hosts without the colons, so it does appear to be doing something.
June 17th, 2009 at 5:21 pm
The example was a dirty one, It may fail if the lines have comments such as #
try this in a terminal
IFS=”:”
read a b c d<<EOF
Hello:World:Apples:Oranges
EOF
echo $a
unset IFS
September 1st, 2009 at 11:34 am
I played around with your script for a while and got it to work after making a few modifications.
#!/bin/bash
IFS=”:”
while read username pass auid agid agroup ahome ashell
do
echo $username
echo $pass
echo $auid
echo $agid
echo $agroup
echo $ahome
echo $ashell
echo “——–”
sleep 1
done < /etc/passwd
exit 0
September 11th, 2009 at 11:09 pm
Array version:
IFS=”:”; while read line; do array=($line); echo ${array[0]}; done < /etc/passwd