Hi Need a shell script to parse through the csv file - Line by line and then field by field ]
the file will look like this
X1,X2,X3,X4
Y1,Y2,Y3,Y4
I need to extract each of these X1,X2....
I wrote a script but it fails if the line exceeds one line..
Here's how I would do it.
First i set the IFS environment variable to tell
read
that "," is the field separator.Given the file "input" containing the data you provided, I can use the following code:
To quickly recap what is going on above.
cat test |
reads the file and pipes it towhile
.while
runs the code betweendo
anddone
whileread
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. Finallyecho
just displays the variables we read.Which gives me the following output
BTW, the BASH manual is always good reading. You'll learn something new every time you read it.
Since eykanal mentioned AWk and and
sed
, I thought I'd show how you could use them.or
Then a shell script could process their output:
Of course, you could do the processing within the AWK script: