need help in Parsing a CSV file and generate a new output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help in Parsing a CSV file and generate a new output file
# 1  
Old 07-26-2008
Question need help in Parsing a CSV file and generate a new output file

Hi Scripting Gurus,
I am trying to parse a csv file and generate a new output file.
The input file will be a variable length in turns of rows and columns.
output file will have 8 columns. we have three columns from the header for each set.
just to give little bit more clarification each row will have n number of sets as seen in the input file we have two sets .
we can have two solutions one is split each row into two rows or bring set2 under set 1 and so on if we have more sets.

|-----------set 1----------|----------set 2----------------|
INPUT FILE
Header,4567,USD,BMI,,4568,USD,BMI,
Date,LEVEL,NET,TOTAL,MCAP,LEVEL,NET,TOTAL,MCAP
22-Dec-97,27.06492573,,20.9289574,,26.17032566,,20.60702136,
23-Dec-97,27.11627283,,20.96866346,,26.50090757,,20.86732796,
24-Dec-97,27.17448579,,21.01367878,,26.53347955,,20.89299111,
25-Dec-97,27.17854775,,21.02091981,,26.66484879,,20.99775394,
26-Dec-97,27.13228338,,20.98513719,,26.55463469,,20.91096371,

Desired output

Date, index_id,currency,index_pro, level, net, total, mcap
22-Dec-97, 4567, USD, BMI, 27.06492573, , 20.9289574 ,
22-Dec-97, 4568, USD, BMI, 26.17032566, , 20.60702136,
23-Dec-97, 4567, USD, BMI, 27.11627283, , 20.96866346,
23-Dec-97, 4568, USD, BMI, 26.50090757, , 20.86732796,
24-Dec-97, 4567, USD, BMI, 27.17448579, , 21.01367878,
24-Dec-97, 4568, USD, BMI, 26.53347955, ,20.89299111,
25-Dec-97, 4567, USD, BMI, 27.17854775, ,21.02091981,
25-Dec-97, 4568, USD, BMI, 26.66484879, ,20.99775394,
26-Dec-97, 4567, USD, BMI, 27.13228338, ,20.98513719,
26-Dec-97, 4568, USD, BMI, 26.55463469, ,20.91096371,


here is the script i wrote and iam having issues geting the proper out put.

Code:
#!/bin/sh

hdr=`cat inputfile.csv|grep 'Header'|awk -F"," '{ t += NF - 1 } END { print t }'`
hdr_str=`cat inputfile.csv|grep 'Header'`
hdr=`expr $hdr + 1`
while read line
do
  i=2;
  j=1;
  dte=`echo $line | awk -F"," '{ print $1 }'`
  k=2;
  str_tmp="";
  l=1;
  while [ $k -le ${hdr} ]
  do
    if [ $l -le 3 ]; then
      str_id_cur_iprv=`echo $hdr_str | awk -F"," '{ print $'$k' }'`
      if [ $l -eq 1 ]; then
        str_tmp=$str_id_cur_iprv
        l=`expr $l + 1`
        k=`expr $k + 1`
      else
        l=`expr $l + 1`
        k=`expr $k + 1`
        str_tmp=$str_tmp","$str_id_cur_iprv
      fi
    else
      break
    fi
  done
  str=${dte}","$str_tmp
  while [ $i -le ${hdr} ]
  do
    if [ $j -le 4 ]; then
      var=`echo $line | awk -F"," '{ print $'$i' }'`
      str=${str}","${var}
      i=`expr $i + 1`
      j=`expr $j + 1`
    else
      echo $str >> output_file.dat
      str=${dte}","$str_tmp
      j=1;
    fi
  done
  if [ $j -ne 1 ]; then
    echo $str >> output_file.dat
    str=${dte}","$str_tmp
  fi
done < inputfile.csv


here is the out put i get with the above script.

Header,4567,USD,BMI,4567,USD,BMI,
Header,4567,USD,BMI,4568,USD,BMI,
Date,4567,USD,BMI,LEVEL,NET,TOTAL,MCAP
Date,4567,USD,BMI,LEVEL,NET,TOTAL,MCAP
22-Dec-97,4567,USD,BMI,27.06492573,,20.9289574,
22-Dec-97,4567,USD,BMI,26.17032566,,20.60702136,
23-Dec-97,4567,USD,BMI,27.11627283,,20.96866346,
23-Dec-97,4567,USD,BMI,26.50090757,,20.86732796,
24-Dec-97,4567,USD,BMI,27.17448579,,21.01367878,
24-Dec-97,4567,USD,BMI,26.53347955,,20.89299111,
25-Dec-97,4567,USD,BMI,27.17854775,,21.02091981,
25-Dec-97,4567,USD,BMI,26.66484879,,20.99775394,
26-Dec-97,4567,USD,BMI,27.13228338,,20.98513719,
26-Dec-97,4567,USD,BMI,26.55463469,,20.91096371,


Gurus can any one take a look at my script and modify it to get the correct form.
Thanks
vkr

Last edited by Franklin52; 07-26-2008 at 01:52 PM.. Reason: code tags added
# 2  
Old 07-26-2008
put your script in code tags
# 3  
Old 07-26-2008
You can do the job with 1 awk command:

Code:
awk 'BEGIN{FS=OFS=",";print "Date, index_id,currency,index_pro, level, net, total, mcap"}
NR==1{s1=$2 FS $3 FS $4; s2=$6 FS $7 FS $8; getline; next}
{printf("%s,%s,%s,%s,%s,\n", $1,s1,$2,$3,$4)}
{printf("%s,%s,%s,%s,%s,\n", $1,s2,$6,$7,$8)}
' inputfile.csv

Regards
# 4  
Old 07-26-2008
Hi Franklin,
Thank you very much. It works.You are a real guru.
As i am a rookie in shell scripting could you please modify the above script so that i will know the mistake that i am making.
Thanks
vkr
# 5  
Old 07-26-2008
hi Franklin,
This is a static script. I want it to be generic where I get variable sets. it can be 2 or 3 or 4 or for that matter any number of sets side by side. could you please give me a generic script where i can generate an out put file.
I appreciate all your help.
Thanks
vkr
# 6  
Old 07-26-2008
hi Franklin,
This is a static script. I want it to be generic where I get variable sets. it can be 2 or 3 or 4 or for that matter any number of sets side by side. could you please give me a generic script where i can generate an out put file.
I appreciate all your help.
Thanks
vkr
# 7  
Old 07-27-2008
Try and adapt the following script :
Code:
awk '

BEGIN {
  OFS = FS = ",";
  Columns_by_set = 4;
  Infos_by_set   = 3; # Output columns count from header for each set
  print "Date","index_id","currency","index_pro",
               "level","net","total","mcap";
}

$1 == "Header" {
   Sets_by_line   = int((NF -1) / Columns_by_set);
   Fields_by_line = NF;

   for (set=1; set<=Sets_by_line; set++) {
      infos = "";
      field = 2 + (set-1)*Columns_by_set
      for (col=1; col<=Infos_by_set; col++) {
         infos = infos $(field+col-1) (col<Infos_by_set ? OFS : "");
      }
      Header_infos[set] = infos;
   }

   next;
}

$1 == "Date" {
   next;
}

Sets_by_line {
   if (NF != Fields_by_line) {
      printf("Invalid datas line %d: Fields count is %d, must be %d\n",
             NR, NF, Fields_by_line);
      next;
   }

   for (set=1; set<=Sets_by_line; set++) {
      datas = $1 OFS Header_infos[set];
      field = 2 + (set-1)*Columns_by_set
      for (col=1; col<=Infos_by_set; col++) {
         datas = datas OFS $(field+col-1);
      }
      print datas
   }
}
' inputfile

Inputfile:
Code:
Header,4567,USD,BMI,,4568,USD,BMI,
Date,LEVEL,NET,TOTAL,MCAP,LEVEL,NET,TOTAL,MCAP
22-Dec-97,27.06492573,,20.9289574,,26.17032566,,20.60702136,
23-Dec-97,27.11627283,,20.96866346,,26.50090757,,20.86732796,
24-Dec-97,27.17448579,,21.01367878,,26.53347955,,20.89299111,
25-Dec-97,27.17854775,,21.02091981,,26.66484879,,20.99775394,
26-Dec-97,27.13228338,,20.98513719,,26.55463469,,20.91096371,
Header,9876,EUR,BMI,
Date,LEVEL,NET,TOTAL,MCAP
22-Dec-97,27.06492573,,20.9289574,
23-Dec-97,27.11627283,,20.96866346,
24-Dec-97,27.17448579,,21.01367878,
25-Dec-97,27.17854775,,21.02091981,
26-Dec-97,27.13228338,,20.98513719,

Output:
Code:
Date,index_id,currency,index_pro,level,net,total,mcap
22-Dec-97,4567,USD,BMI,27.06492573,,20.9289574
22-Dec-97,4568,USD,BMI,26.17032566,,20.60702136
23-Dec-97,4567,USD,BMI,27.11627283,,20.96866346
23-Dec-97,4568,USD,BMI,26.50090757,,20.86732796
24-Dec-97,4567,USD,BMI,27.17448579,,21.01367878
24-Dec-97,4568,USD,BMI,26.53347955,,20.89299111
25-Dec-97,4567,USD,BMI,27.17854775,,21.02091981
25-Dec-97,4568,USD,BMI,26.66484879,,20.99775394
26-Dec-97,4567,USD,BMI,27.13228338,,20.98513719
26-Dec-97,4568,USD,BMI,26.55463469,,20.91096371
22-Dec-97,9876,EUR,BMI,27.06492573,,20.9289574
23-Dec-97,9876,EUR,BMI,27.11627283,,20.96866346
24-Dec-97,9876,EUR,BMI,27.17448579,,21.01367878
25-Dec-97,9876,EUR,BMI,27.17854775,,21.02091981
26-Dec-97,9876,EUR,BMI,27.13228338,,20.98513719

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate .csv file

Dears,I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the file as an input the file will be... (8 Replies)
Discussion started by: abdul2020
8 Replies

2. Shell Programming and Scripting

Script to generate csv file

Dears, I am new in shell world and I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the... (3 Replies)
Discussion started by: abdul2020
3 Replies

3. Shell Programming and Scripting

Generate .csv file a text file

Hi guys, I have a text file that states: cat prod.hosts 11.29.130.14 host1 host1.test.com web17 11.29.130.15 host2 host2.test.com web18 11.29.130.16 host3 host3.test.com web19 I want a .csv file that states: ... (2 Replies)
Discussion started by: Junaid Subhani
2 Replies

4. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

5. Shell Programming and Scripting

Script to generate csv file

Hello; I need to generate a csv file that contains a list of all the files in a particular server (from the root directory ie: \) that have a permission stamp of 777. I would like to create the csv so that it contains the following: server name, file name, full path name where file exists,... (17 Replies)
Discussion started by: gvolpini
17 Replies

6. Shell Programming and Scripting

Read a CSV file and generate SQL output

Friends, This is what I need: I will pass a CSV file as an input, and I want my shell to be reading that CSV file, and based on the parameters it should generate SQLs and write those SQL in a different file in the same location. I'm new to Shell scripting. I'm currently working on a... (25 Replies)
Discussion started by: Ram.Math
25 Replies

7. Shell Programming and Scripting

to read a CSV file and generate SQL output

Friends, This is what I need: I will pass a CSV file as an input, and I want my shell to be reading that CSV file, and based on the parameters it should generate SQLs and write those SQL in a different file in the same location. I'm new to Shell scripting. I'm currently working on a... (1 Reply)
Discussion started by: Ram.Math
1 Replies

8. Shell Programming and Scripting

Need to generate .csv file

I have a csv file with the following data Please find the attachment - zip ... (6 Replies)
Discussion started by: vaas
6 Replies

9. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

10. Shell Programming and Scripting

Generate csv file

I have a file which has some thousand records in the following format File: input.txt -> <option value="14333">VISWANADH VELAMURI</option> <option value="17020">VISWANADHA RAMA KRISHNA</option> I want to generate a csv file from the above file as follows File: output.txt -> ... (4 Replies)
Discussion started by: rahulrathod
4 Replies
Login or Register to Ask a Question