Shell script to extract data from csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to extract data from csv file
# 8  
Old 09-03-2013
Again your output file description does not match your output sample nor can the output file be obtained from your sample input files.

Please do not leave people guessing. Show a representative sample of input, desired output, attempts at a solution and specify OS and versions being used, or this thread will be closed.
# 9  
Old 09-04-2013
Script to extract data from a file

Hi everyone,

I have a csv file which has data with different heading and column names as below.It has few null columns

Code:
Static Data Ingested
,,,,,,,,,,,,Known Explained Rejections
Column_1,column_2,Column_3,Column_4,,Column_6,Column_7,,% Column_8,,Column_9 ,Column_10.
viv,Public_456,08/28/2013,1234566,,08/31/2013,5896325,,0.00,,0,1
SDS,Public_ddd,08/28/2013,589652,,08/31/2013,2365896,,0.00,,0,0

Code:
Static Data extracted
Column_1,Column_2,Column_3,Column_4,Column_5
rms,k2c,0.00,0,0
SDS,k3c,0.00,0,1

The above two records are present in the same csv file.
First data heading is Static Data Ingested and is at line 1.
At line 2 is ,,,,,,,,,,,,Known Explained Rejections
Line 3 has column names and line 4,5 and so on has multiple rows containing the values.

After few lines/rows say 20 start the second heading Static Data extracted,then its columns and then its values.

Now i want to check if in first heading column 9 and 10 have any other value than 0,output should be as below:

Code:
Sample output:
Static Data Ingested:
Column_1,column_2,Column_3 Column_9 ,Column_10
SDS,k3c,0.00,0,1

Code:
Static Data extracted
Column_1,Column_2,Column_3,Column_4,Column_5
viv,Public_456,08/28/2013,0,1

Similarily for second heading 0's should be checked at column 4 and 5.
All the out for both first and second should be sent in a mail containing both the headings(Static Data Ingested and Static Data extracted) along with their columna nd respective values.
# 10  
Old 09-04-2013
I am completely lost by your description of this problem.

Your "Static Data Ingested" input has 12 fields with some columns unlabeled and some with labels that do not match what is in your desired output. The output that you say is expected for "Static Data Ingested" has four fields with headings Column_1, column_2, Column_3 Column_9 , and Column_10, but you show five comma separated output fields. There is no heading column_2 (with a lowercase "c") in your input, there is no heading Column_3 Column_9 in your input, and there is a Column_10. (with a trailing period) in your input, but not a Column_10 heading. The data line in your input starting with SDS:
Code:
SDS,Public_ddd,08/28/2013,589652,,08/31/2013,2365896,,0.00,,0,0

has zero (or null) values in input columns 9 and 10 and in the columns labeled Column_9 (with the trailing space) and Column_10. (with the trailing period). But, you said there should only be output for this line if the values are not 0. And, how did you get the output:
Code:
SDS,k3c,0.00,0,1

from the input line above.

Then, I don't see any way to get the output:
Code:
viv,Public_456,08/28/2013,0,1

from the input:
Code:
rms,k2c,0.00,0,0
SDS,k3c,0.00,0,1

in the "Static Data extracted" sections of your input and output files???

What have you tried so far to solve this problem?

How are the field numbers or field names to be evaluated supposed to be entered into whatever script is supposed to perform this task for you?

How are the field numbers or field names that are supposed to be output entered into whatever script is supposed to perform this task for you?

Is an empty field to be treated as having value 0?

Is a field containing 0.00 to be treated as 0? (I.e., is it a numeric comparison or a string comparison?)
# 11  
Old 09-04-2013
Threads merged
# 12  
Old 09-05-2013
Can anyone help me how to get the data from the files along with respective headers.sorry for the confusion..again is the below of input file and output file.,, indicates olumn have null values.



Code:
Static Data Ingested
,,,,,,,,,,,,Known Explained Rejections
Column_1,column_2,Column_3,Column_4,column_5,Column_6,Column_7,% Column_8,Column_9 ,Column_10.
viv,Public_456,08/28/2013,1234566,,08/31/2013,,0.00,,0,1
SDS,Public_ddd,08/28/2013,589652,,08/31/2013,,0.00,,0,0

Code:
Static Data extracted
Column_1,Column_2,Column_3,Column_4,Column_5
rms,k2c,0.00,0,0
SDS,k3c,0.00,0,1

The output should display only the records having column 10 as 1 in first heading(static data ingested) and column 5 as 1 in second heading(static data extracted).The output should be as below.

Code:
Static Data Ingested:
Column_1,column_2,Column_3,Column_4,column_5,Column_6,Column_7,% Column_8,Column_9 ,Column_10
viv,Public_456,08/28/2013,1234566,,08/31/2013,,0.00,,0,1

Static Data extracted
Column_1,Column_2,Column_3,Column_4,Column_5
SDS,k3c,0.00,0,1


In the input file at line 1 is heading,at line 2 another heading and line 3 are the column names and line 4 are the values of columns.
Then after few lines say 10 again there is a new heading then column names and then column values.
# 13  
Old 09-05-2013
Your data in the first part has 11 columns, not 10.

So using column 11 in the variable s:

Code:
$ awk '/Static Data extracted/{s=t; getline; print} NR==3||$s==1' s=11 t=5 FS=, file
Column_1,column_2,Column_3,Column_4,column_5,Column_6,Column_7,% Column_8,Column_9 ,Column_10.
viv,Public_456,08/28/2013,1234566,,08/31/2013,,0.00,,0,1
Column_1,Column_2,Column_3,Column_4,Column_5
SDS,k3c,0.00,0,1

Please make sure your description and your sample data match...

Last edited by Scrutinizer; 09-05-2013 at 02:08 PM..
# 14  
Old 09-05-2013
Hi..Thnaks for the reply.
But how we will get two headings both
Code:
Static Data Ingested and Static Data extracted

each having its own columns and its respective values.

Also consider there are 11 columns in total in first heading.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to extract data in a file

I have this 5GB file, and i want to extract from the file particulars pattern. this is my script: // count=`grep -wc "MSISDN" file_name` k=1 >OUTPUT >OUTPUT_Final while do cat file_name | awk -F":" -v var="$k" '$1=="MSISDN" {m++}m==var{print; exit}' >> OUTPUT cat file_name |awk -F":"... (33 Replies)
Discussion started by: gillesi
33 Replies

2. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 7 columns having values say column 1,column 2.....column 7 as below along with their values. Name, Address,... (7 Replies)
Discussion started by: Vivekit82
7 Replies

3. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file based on certain conditions

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (1 Reply)
Discussion started by: Vivekit82
1 Replies

4. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

5. Shell Programming and Scripting

How to extract data from csv file

Hello everybody, Here is my problem, I don't know anything about shell programming and my boss is actually asking me to develop a shell script in order to get values in a csv file from a specific date. Here is a sample of the csv file : Date;Enchaînement;Titre;Libellé ;calendrier;Heure début;Heure... (11 Replies)
Discussion started by: freyr
11 Replies

6. Shell Programming and Scripting

need a shell script to extract data from a log file.

If I have a log like : Mon Jul 19 05:07:34 2010; TCP; eth3; 52 bytes; from abc to def Mon Jul 19 05:07:35 2010; UDP; eth3; 46 bytes; from aaa to bbb Mon Jul 19 05:07:35 2010; TCP; eth3; 52 bytes; from def to ghi I will need an output like this : Time abc to def... (1 Reply)
Discussion started by: hitha87
1 Replies

7. Shell Programming and Scripting

Exporting data as a CSV file from Unix shell script

Friends...This is the first time i am trying the report generation using shell script... any suggestions are welcome. Is there a way to set the font size & color when i am exporting the data from unix shell script as a CSV file ? The following sample data is saved as a .csv file in the... (2 Replies)
Discussion started by: appu2176
2 Replies

8. Shell Programming and Scripting

Help with shell script to extract data from XML file

Hello Scripting Gurus, I need help with extracting data from the XML file using shell script. The data is in a large XML and I need to extract the id values of all completedworkflows. Here is a sample of it. Input and output data is also in the attached text files. <wfregistry>... (5 Replies)
Discussion started by: yajaykumar
5 Replies

9. Shell Programming and Scripting

shell-script which extract data from log file

give me a shell-script which extract data from log file on a server by giving date and time as input (for both start time and end time) and it will give the logs generated during the given time as output. (4 Replies)
Discussion started by: abhishek27
4 Replies

10. Shell Programming and Scripting

Shell Script to Load data into the database using a .csv file and .ctl file

Since i'm new to scripting i'm findind it difficult to code a script. The script has to be an executable with 2 paramters passed to it.The Parameters are 1. The Control file name(.ctl file) 2. The Data file name(.csv file) Does anybody have an idea about it? :confused: (3 Replies)
Discussion started by: Csmani
3 Replies
Login or Register to Ask a Question