LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   consolidating rows from csv file (https://www.linuxquestions.org/questions/linux-newbie-8/consolidating-rows-from-csv-file-918677/)

noony123 12-14-2011 06:08 AM

consolidating rows from csv file
 
Hi all. I have a csv file with complete rows like this
(xxx is just random value but is needed)
0801 xxxx xxxx xxxx
0801 xxxx xxxx xxxx
0801 xxxx xxxx xxxx
0801 xxxx xxxx xxxx
0801 xxxx xxxx xxxx
0851 yyyy yyyy yyyy
0851 yyyy yyyy yyyy
0851 yyyy yyyy yyyy
0851 yyyy yyyy yyyy
0851 yyyy yyyy yyyy

(xxxx values in all rows are distinct and unique, i just wrote xxx for simplicity)

Now i want to consolidate all rows containing 0801 to a csv file named 0801, same with 0851 with 0851.csv file. But need to automate this process, is it possible using any builtin linux tool like awk or something ? kindly guide me

zQUEz 12-14-2011 06:34 AM

Does this achieve what you are after:
Code:

for i in `cat input.file |cut -d" " -f1 |sort -u`; do cat input.file |grep ^$i >$i; sed -i 's/ /,/g' $i; done
this assumes your origonal csv is called "input.file".

colucix 12-14-2011 07:08 AM

Code:

awk '{ print > $1 ".csv" }' file

AwesomeMachine 12-14-2011 07:47 AM

You could try

#!/bin/bash
$ tr -t " " "," < input.file > output.file
if grep '0801' output.file
then `grep '0801' output.file` >> 0801.csv
elif grep '0851' output.file
`grep '0851' output.file` >> 0851.csv
else
echo "done"
fi
exit 0


All times are GMT -5. The time now is 01:35 PM.