Using awk command for .csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk command for .csv file
# 1  
Old 11-30-2009
MySQL Using awk command for .csv file

Hi ,
I have .csv file with value separated by ";".
1. Using awk how to extract perticular colums and store in to array
2. For some columns I needs to extract last value of the column

How to do same please help me ASAP

Thanks and Regards,
Sushma
# 2  
Old 11-30-2009
Quote:
Originally Posted by sushmab82
Hi ,
I have .csv file with value separated by ";".
1. Using awk how to extract perticular colums and store in to array
2. For some columns I needs to extract last value of the column

How to do same please help me ASAP

Thanks and Regards,
Sushma
Please post a sample input & expected output
# 3  
Old 11-30-2009
You could use something like this,


cat tst.csv | awk -F";" '{v1=$0;split(v1,names,";");for (idx=1;idx<NF+1;idx++) print idx, names[idx];}'

The input file is,
$ cat tst.csv
1;2;3;4;5;6;7;8
a;b;c;d;e;f;g;h

And the output is,
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h

Hope it helps.
# 4  
Old 12-02-2009
Hi Dennis,
Please find sample input and output

input:
StartTime;LastResetTime;CurrentTime;ElapsedTime(P);ElapsedTime(C);Rate(P);Rate(C);IncomingCall(P);In comingCall(C);OutgoingCall(P);OutgoingCall(C);MsgRecvPerS(P);MsgRecvPerS(C);MsgSendPerS(P);MsgSendPe rS(C);UnexpectedMsg(P);UnexpectedMsg(C);CurrentCall;InitSuccessfulCall(P);InitSuccessfulCall(C);Traf ficSuccessfulCall(P);TrafficSuccessfulCall(C);DefaultSuccessfulCall(P);DefaultSuccessfulCall(C);Abor tSuccessfulCall(P);AbortSuccessfulCall(C);FailedCall(P);FailedCall(C);FailedRefused(P);FailedRefused (C);FailedAborted(P);FailedAborted(C);FailedTimeout(P);FailedTimeout(C);ResponseTime(P);ResponseTime (C);ResponseTimeRepartition;<50;<75;<100;<150;<300;<5000;>=5000;
2009-10-23 16:55:14;2009-10-23 16:55:14;2009-10-23 16:55:14;00:00:00;00:00:00;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;00:00:00:000;00 :00:00:000;;0;0;0;0;0;0;0;
2009-10-23 16:55:14;2009-10-23 16:55:15;2009-10-23 16:55:15;00:00:01;00:00:01;1.998;1.99005;0;0;2;2;1.998;1.99005;2.997;2.98507;1;1;1;1;1;0;0;0;0;0;0;0 ;0;0;0;0;0;0;0;00:00:00:439;00:00:00:439;;0;0;0;0;0;1;0;

Output :
array_16 = column16
last_value = column_16_lastrecord

---------- Post updated at 12:28 AM ---------- Previous update was at 12:20 AM ----------

Hi Shan,
Thanks for ur reply.
Since .csv file containes 50 lacs of records, If I use for loop and store into array takes too much of time. And I don't want to store all coulmns data, only selected column I need to store. Please suggest if any method of doing without iterating to each row of the file.
# 5  
Old 12-02-2009
first in your sample file, only 45 column, if split by ;

Code:
$ awk -F\; '{print NF}' input
45
45
45

If you need output selected columns, you can use below ways:

Code:
awk -F\; '{print $3,$10,$30}' input
cut -d\; -f3,10,20-30,42- <input

Still I don't get how you get this output from your input file, can you explain on it?

Code:
array_16 = column16
last_value = column_16_lastrecord

# 6  
Old 12-02-2009
Hi,
I need last record(last row value) of column 16. Suppose column 16 has 2 records(2 rows) I want to grep and store 2nd record of column16 i.e 3 in my input file.
# 7  
Old 12-02-2009
Sushma,
If I understand correctly then, for your 2nd query you can simply do a tail -1 and then cut the field whose value you require,

e.g. to get the last row value in the first field,

tail -1 file.csv | cut -d';' -f1

Regards,
Shantanu


With awk you can do,
awk -F";" 'END {print $1}' file.csv

Last edited by Shan_u2005; 12-02-2009 at 05:29 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What does the sed command here exactly doing to csv file?

I have the following csv file at the path; now using sed command. what is this exactly doing. sed -i 's//,/g' /FTP/LAB_RESULT_CM.csv Thank you very much for the helpful info. (2 Replies)
Discussion started by: cplusplus1
2 Replies

2. Shell Programming and Scripting

awk command to manipulate csv file in UNIX

Hi, I am new to awk and unix programming and trying to manipulate a csv file. My current csv file looks like this: col1,col2,col3,col4,col5,col4,col5,col6,col7,col8 223,V,c,2,4,f,r,,y,z 223,V,c,3,2,f,r,,y,z 223,V,c,1,4,f,r,,y,z 223,V,c,4,3,f,r,,y,z 227,V,c,3,1,f,r,,y,z... (8 Replies)
Discussion started by: Prit Siv
8 Replies

3. Shell Programming and Scripting

awk command to manipulate csv file in UNIX

Hi, I am new to awk/unix and am trying to put together a script to manipulate the date column in a csv file. I have file1.csv with the following contents: Date,ID,Number,Amount,Volume,Size 01-Apr-2014,WERFG,998,105873.96,10873.96,1342.11 01-Apr-2014,POYFR,267,5681.44,5681.44,462.96 I... (2 Replies)
Discussion started by: Prit Siv
2 Replies

4. Shell Programming and Scripting

Csv file separate using awk

Hi, I have file like below apple,orange,pineapple,pappya,guva,avocado want to store as apple orange pineapple pappya I tried below command to seprate first field command1: (3 Replies)
Discussion started by: stew
3 Replies

5. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

6. UNIX for Dummies Questions & Answers

xml to csv using sed and awk command

Hi Guys, Can you help me in creating shell script using sed,awk etc commands to generate csv file using xml file. (5 Replies)
Discussion started by: sbk
5 Replies

7. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

8. Shell Programming and Scripting

awk/sed/something else for csv file

Hi, I have a filename.csv in which there are 3 colums, ie: Name ; prefixnumber ; number root ; 020 ; 1234567 user1,2,3 ; 070 ; 7654321 What I want is to merge colum 2 and 3 that it becomes 0201234567 or even better +31201234567 so the country number is used and drop the leading 0.... (9 Replies)
Discussion started by: necron
9 Replies

9. Shell Programming and Scripting

Awk to convert a flat file to CSV file

Hi , I have a file with contents as below: Contract Cancellation Report UARCNCL LOS CODE DATE REAS TYPE AMOUNT AMOUNT LETTER BY ========= ======= ==== ==== ==== ========= ==== ==== 8174739 7641509 1S NONE CRCD 30-JUN-2008 NPAR N .00 .00 CCAN 8678696 8091709 1S NONE DDEB 30-JUN-2008... (14 Replies)
Discussion started by: rkumudha
14 Replies

10. Shell Programming and Scripting

creating a csv file in awk

Hi All I am trying to create a csv file in the korn shell and the script segment is as follows: if then # NEED TO ADD INFO TO THE EMAIL FILE ABOUT THE DRIVE THAT'S FILLING UP echo "$drive $percent% $space "|\ awk '{printf("%d/t"|"%d/t"|"%d/t\n",... (6 Replies)
Discussion started by: Segwar
6 Replies
Login or Register to Ask a Question