Need help with CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with CSV file
# 1  
Old 07-21-2011
Need help with CSV file

Hi Guys
I have a file which have some data..
I want to fetch some data and creating csv file from that file

Input file format
Code:
20/11/11
SQL CD : 100
query
select name,class from table school

I have to create a csv file which have these columns
date,sql code,entire text (entire file data)
so I m using this code
Code:
date=`head -1 file`
sql=`head -2 file | tail -1`
fullfile=`cat file`

echo $date,$sql,$fullfile > CSV_file

but when I open this file in excel with comma ( , ) separate. The SQL QUERY gave the problem

because SQL query also have some commas ( , ) so the last column which is fullfile is divided into 2 or 3 columns depending on the commas in SQL query

please help me to resolve this
# 2  
Old 07-21-2011
Then try other de-limiters like semicolon ( ; ) while doing echo in your script instead of comma. I guess you know how to open the file in excel with delimiter as semi-colon.
# 3  
Old 07-28-2011
thanks man.. its worked... Smilie
but here is the my actual problem... please help...

I have this input file
Code:

                           SYSTEMS AREA

                   PRODUCTION PROBLEM/ABEND DATA


 1.  RUN DATE: 06/06/11  2. SYSTEM:   EP0Z     3. PROBLEM TYPE: S222
               --------               ----                      ----
 4.  JOB NUMBER:  J9140  5. JOB NAME: EP0Z080P 6. STEPNAME: EPZSTP06
                  -----               --------              --------
 7.  TIME NOTIFIED: 01:17       8. WHO NOTIFIED: VAIBHAV MATHUR
                    -----------                  -------------------
                       9. HELPERS:
                                   ---------------------------------

 10. WHAT WAS WRONG:

   JOB WAS LOOPING :- RUNNING SINCE 19.54 SO I CANCELLED AT 01:17 ON         RED
   06/07/11

 11. WHEN WAS THE PROBLEM CORRECTED:

     CHCECKED EARLIER SCENARIO IN EP00T.ABEND.DATA WHICH OCCURRED ON
     02/02/2011. BASED ON THAT, DELETED THE SUBSCRIBER FROM INPUT FILE
     EP00T.EPZ00080.INPUT.TRIGG.FEB0211G

     1) REPORT :- EPZ00804, LAST PROCESS RECORD AND NEXT RECORD.
       142924152
       142924164
     2) REPORT :- EPZ00802, LAST PROCESS RECORD AND NEXT RECORD.
       141844527
       141844665
     3) REPORT :- EPZ00803, LAST PROCESS RECORD AND NEXT RECORD.
       141460794
       141461097

     JOB WAS RESTARTED FROM THE ABENDED STEP AT 02:16
     WITH OVERRIDE
     //EPZSTP06.TRIGGFLI DD DSN=EP00T.EPZ00080.INPUT.TRIGG.JUN0711

      JOB AGAIN WENT INTO INFINITE LOOP. SUBSEQUENTLY IDENTIFIED THE
      SUBSCRIBER CAUSING THE ISSUE, USING THE BELOW SET OF QUERIES -

      STEP 1 -
      FOUND THE TIMESTAMP OF LAST DAY'S RUN USING QUERY
      SELECT * FROM PROD01.BTCH_CYCL_PARM
      WHERE CYCL_TYP_CD = 'NMS'
      ORDER BY CYCL_STRT_TMP DESC
      WITH UR;
      TIMESTAMP WAS 2011-06-04-18.01.08.922740

      STEP 2 -
      SELECT SUBS_ID, MAIN_GRP_ID, SGRP_ID
       FROM PROD01.SUBS_PRD
      WHERE PRD_ID LIKE 'P%'
        AND SP_LST_UPD_TMP > '2011-06-04-18.01.08.922740'
      GROUP BY SUBS_ID, MAIN_GRP_ID, SGRP_ID
      HAVING COUNT(*) >= 3
      WITH UR;

      THIS QUERY GAVE FOLLOWING RESULT -
      SUBS_ID     MAIN_GRP_ID  SGRP_ID
      143222959   00001Y       006
      153705667   05938S       078

      STEP 3 -
      FOR BOTH THESE SUBSCRIBERS, EXECUTED FOLLOWING QUERY:
      SELECT * FROM PROD01.SUBS_PRD
      WHERE MAIN_GRP_ID = '00001Y' AND SGRP_ID = '006' AND SUBS_ID =
      '143222959'
        AND PRD_ID LIKE 'P%' AND SP_LST_UPD_TMP >
      '2011-06-04-18.01.08.922740'
      UNION ALL
      SELECT * FROM PROD01.SUBS_PRD_SS
      WHERE MAIN_GRP_ID = '00001Y' AND SGRP_ID = '006' AND SUBS_ID =
      '143222959'
        AND PRD_ID LIKE 'P%' AND SP_LST_UPD_TMP >
      '2011-06-04-18.01.08.922740'
      ORDER BY SP_LST_UPD_TMP DESC WITH UR
      SIMILAR QUERY FOR THE OTHER SUBSCRIBER.

     RESULT -

     MAIN
     GRP     SGRP  PRD      SUBS         SP          SP
      ID      ID   ID        ID         EFDT        TRMDT
     ------  ----  -------  ----------  ----------  ----------
     00001Y  006   P1006    143222959   05/01/2009  06/01/2011
     00001Y  006   P1006    143222959   05/01/2009  12/31/9999
     00001Y  006   P1006    143222959   02/01/2009  05/01/2009
     00001Y  006   P1006    143222959   01/01/2009  01/01/2009
     00001Y  006   P1006    143222959   05/01/2009  05/01/2009


     MAIN
     GRP     SGRP  PRD      SUBS         SP          SP
      ID      ID   ID        ID         EFDT        TRMDT
     ------  ----  -------  ----------  ----------  ----------
     05938S  078   P1615    153705667   06/01/2011  12/31/9999
     05938S  078   P1615    153705667   05/07/2011  06/01/2011
     05938S  078   P1615    153705667   05/07/2011  12/31/9999
     05938S  078   P1615    153705667   10/28/2010  05/07/2011

     STEP 4 -
     CHECK BELOW CONDITIONS-
     - EFFECTIVE DATE OF THE FIRST RECORD SHOULD BE GREATER THAN 3RD OR
       GREATER RECORD.
     - 1ST RECORD SHOULD NOT BE A NULLED RECORD.

     OF THESE TWO SUBSCRIBERS, SUBSCRIBER 143222959 DOESNT SATISFY
     FIRST CONDITION.
     THE FIRST ROW HAS AN EFFECTIVE DATE 05/01/2009, AND ALSO THE 5TH
     ROW HAS THE SAME EFFECTIVE DATE.
     THE 1ST ROW'S DATE SHOULD IDEALLY BE GREATER TO AVOID THIS ISSUE.

     STEP 5 -
     DELETED SUBSCRIBER 143222959 FROM THE ORIGINAL INPUT FILE, AND
     PROVIDED FOLLOWING OVERRIDE -
     JOB WAS RESTARTED FROM THE ABENDED STEP AT 03:48
     WITH OVERRIDE
     //EPZSTP06.TRIGGFLI DD DSN=EP00T.EPZ00080.INPUT.TRIGG.JUN0711B

     THE JOB COMPLETED SUCCESSFULLY THIS TIME AT 04:27

 13. FINAL STATUS:

     THE JOB COMPLETED TO CREATE OUTPUT FILE FOR NDM TO CAREMARK
     SUCCESSFULLY.


 14. PRODUCTION SUPPORT RESPONSE:
                                                    SC TICKET# IM213122

and need this type of output
please click on the link to see the output file...

http://i52.tinypic.com/2gwcs61.jpg

I have stuckd with "Problem" and "Incident and Solution" column because we need it as this is in input file.

I wrote this code..
Code:
#!/bin/ksh

filename=`ls $1 | grep -v ".*.csv"`
output=output`date +%m%d%Y`.csv
cd $1
echo $filename

ls $filename|while read filename
do
jdate=`grep 'RUN DATE' $filename | awk '{print $4}'`
jtime=`grep 'TIME NOTIFIED' $filename | awk '{print $4}'`
jname=`grep "JOB NAME" $filename | awk '{print $8}'`
jstepname=`grep "STEPNAME" $filename | awk '{print $11}'`
jprogramname=`grep "PROGRAM" $filename | awk '{print $6}'`
jproblemtype=`grep 'PROBLEM TYPE:' $filename | awk '{print $11}'`
jsqlcode=`sed -nr 's/.*SQL CD : *([-+]*[0-9]*).*/\1/p' $filename | head -1`

jproblem=`sed -n '/10\./,/11\./ p' $filename | egrep -v "10\.|11\.|^$|------------------" | tr "\n" '\.'`
jtemp=`cat $filename`

echo `cat $filename` > temp

jtotal=`cat temp`


if [[ `ls | grep $output` ]]
then
echo File is exist.
else
echo File is not exist.
echo 'Job Date'';''Job Time'';''Job Name'';''Step Name'';''Program Name'';''Error Code'';''SQL CODE'';''Problem'';''Incident & Solution' > $output
fi



echo -e "\nStart appending......"
echo "$jdate"';'"$jtime"';'"$jname"';'"$jstepname"';'"$jprogramname"';'"$jproblemtype"';'"$jsqlcode"';'"$jproblem"';'"$jtotal" >>$output
echo  -e "done\n"
done

rm temp

I am trying to put the last two column as I need but not successful.
please help

thanks in advance,,, :-)

---------- Post updated 07-28-11 at 03:22 PM ---------- Previous update was 07-27-11 at 10:12 PM ----------

guys please help !!
# 4  
Old 07-28-2011
Hi,

Test this 'perl' script. The output should be a 'csv' file with the pipe character ('|') as separator of fields. The text of your last post is the content of 'infile' in my command.
Code:
$ cat script.pl
use warnings;
use strict;

my %csv;
my @headers = (
        [ 'run date' => 'Job Date' ],
        [ 'time notified' => 'Job Time' ],
        [ 'job name' => 'Job Name' ],
        [ 'stepname' => 'Step Name' ],
        [ 'program name' => 'Program Name' ],
        [ 'problem type' => 'Error Code' ],
        [ 'sql code' => 'SQL Code' ],
        [ 'problem' => 'Problem' ],
        [ 'solution' => 'Incident & Solution' ]
);


while ( my $line = <> ) {

        ## Get headers and its data between points 1 and 10.
        if ( $line =~ /^\s*1\./ .. $line =~ /^\s*$/ ) {
                chomp $line;
                next unless $line =~ /^\s*\d+\./;
                my @points = split /\s*\d+\.\s+/, $line;
                for my $point ( @points ) {
                        next if index( $point, ":" ) == -1;
                        my ($h, $d) = split /\s*:\s+/, $point;
                        $csv{ $h } = $d;
                }
        }

        ## Get header and its data of point 10.
        if ( ( $line =~ /^\s*10\./ .. $line =~ /^\s*11\./ ) =~ /^\d+(?<!^1)$/ ) {
                $csv{ PROBLEM } .= $line;
        } 

        ## Get header and its data of point 11.
        if ( ( $line =~ /^\s*11\./ .. $line =~ /^\s*13\./ ) =~ /^\d+(?<!^1)$/ ) {
                $csv{ SOLUTION } .= $line;
        }
}

## Remove leading and trailing spaces from next headers.
$csv{ PROBLEM } =~ s/^\s*(.*\S)\s*$/$1/s;
$csv{ SOLUTION } =~ s/^\s*(.*\S)\s*$/$1/s;

## Print header.
print join "|", map { ${$headers[$_]}[1] } 0 ..$#headers;
print "\n";

## Print lines of data.
print join "|", map { $csv{ uc ${$headers[$_]}[0] } || "" } 0 ..$#headers;
print "\n";
$ perl script.pl infile
... output suppressed ...

Regards,
Birei
# 5  
Old 07-29-2011
Hi atul9806, I have modified Modified your code.
Code:
#!/bin/ksh

filename=`ls $1 | grep -v ".*.csv"`
output=output`date +%m%d%Y`.csv
cd $1
echo $filename

ls $filename|while read file
do
jdate=`grep 'RUN DATE' $file | awk '{print $4}'`
jtime=`grep 'TIME NOTIFIED' $file| awk '{print $4}'`
jname=`grep "JOB NAME" $file| awk '{print $8}'`
jstepname=`grep "STEPNAME" $file | awk '{print $11}'`
jprogramname=`grep "PROGRAM" $file | awk '{print $6}'`
jproblemtype=`grep 'PROBLEM TYPE:' $file | awk '{print $11}'`
jsqlcode=`sed -nr 's/.*SQL CD : *([-+]*[0-9]*).*/\1/p' $file | head -1`

jproblem=`sed -n '/10\./,/11\./ p' $file | egrep -v "10\.|11\.|^$|------------------" | tr "\n" '\.'`
jtemp=`cat $file`

echo `cat $file` > temp

jtotal=`cat temp`


if [ -f $output ]
then
echo File is exist.
else
echo File is not exist.
echo 'Job Date'';''Job Time'';''Job Name'';''Step Name'';''Program Name'';''Error Code'';''SQL CODE'';''Problem'';''Incident & Solution' > $output
fi



echo -e "\nStart appending......"
echo "$jdate"';'"$jtime"';'"$jname"';'"$jstepname"';'"$jprogramname"';'"$jproblemtype"';'"$jsqlcode"';'"$jproblem"';'"$jtotal" >>$output
echo  -e "done\n"
done

rm temp

# 6  
Old 07-29-2011
thanks pravin
But your script is not giving the output as I want.
As I told here I have problem with last 2 columns

let me give you a example ....

from my script I got the output like this...


Code:
Problem
this is a problem I want the output as I have in my file. but this gave me in one line

but I want like this


Code:
Problem
this is a problem I want the output as I have in my file. 
but this gave me in one line

means I want multilines in a cell.
# 7  
Old 07-29-2011
Quote:
Originally Posted by atul9806
from my script I got the output like this...

Code:
Problem
this is a problem I want the output as I have in my file. but this gave me in one line

but I want like this


Code:
Problem
this is a problem I want the output as I have in my file. 
but this gave me in one line

means I want multilines in a cell.
Use the fmt command to format lines. See man fmt for a reference.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself

Hi, all I want to sort a csv file based on timestamp from oldest to newest and save the output as csv file itself. Here is an example of my csv file. test.csv SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0739.JPG,2015:02:17 11:32:21 /home/intannf/foto/IMG_0749.JPG,2015:02:17 11:37:28... (10 Replies)
Discussion started by: refrain
10 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

5. Shell Programming and Scripting

Compare 2 csv files in ksh and o/p the difference in a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 The... (7 Replies)
Discussion started by: Naresh101
7 Replies

6. Shell Programming and Scripting

Comparing 2 CSV files and sending the difference to a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 ... (1 Reply)
Discussion started by: Naresh101
1 Replies

7. Shell Programming and Scripting

Reading from a CSV and writing in same CSV file

Hi, I am tryng to read from a csv file and based on some grep command output I will modify one of the column in the same csv. Example:- Input CSV:- 20120829001415,noneAA,google.com 20120829001415,dfsafds,google.com 20120829001415,noneAA,google.com Intermediate Step:- If 2nd column of... (3 Replies)
Discussion started by: kmajumder
3 Replies

8. 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

9. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies
Login or Register to Ask a Question