Balualways Balualways - 1 year ago 55
Bash Question

Shell script to parse through a file ( csv ) and process line by line

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


I need to extract each of these X1,X2....

I wrote a script but it fails if the line exceeds one line..

Answer Source

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


BTW, the BASH manual is always good reading. You'll learn something new every time you read it.