Here's how I would do it.
First i set the IFS environment variable to tell read
that "," is the field separator.
export IFS=","
Given the file "input" containing the data you provided, I can use the following code:
cat test | while read a b c d; do echo "$a:$b:$c:$d"; done
To quickly recap what is going on above. cat test |
reads the file and pipes it to while
. while
runs the code between do
and done
while read
returns true. read
reads a line from standard input and separates it into variables ("a", "b", "c" and "d") according to the value of $IFS. Finally echo
just displays the variables we read.
Which gives me the following output
X1:X2:X3:X4
Y1:Y2:Y3:Y4
BTW, the BASH manual is always good reading. You'll learn something new every time you read it.