awk script to convert a text file into csv format


 
Thread Tools Search this Thread
Top Forums Programming awk script to convert a text file into csv format
# 1  
Old 09-15-2010
awk script to convert a text file into csv format

hi...... thanks for allowing me to start a discussion
i am collecting usb usage details of all users and convert it into csv files so that i can export it into some database..
the input text file is as follows:-
Code:
USB History Dump
by nabiy (c)2008 
(1) --- Kingston DataTraveler 130 USB Device 
instanceID: 0018F3D974B4A9C0E1760896&0
ParentIdPrefix:  7&b62e00e&2
Last Mounted As:  \DosDevices\I:
Driver:{4D36E967-E325-11CE-BFC1-08002BE10318}\0033
Disk  Stamp: 09/07/2010 15:07
Volume Stamp: 09/07/2010 15:07 
(2) --- Kingston DataTraveler 2.0 USB Device 
instanceID: 001D0F1E35B25B8C1201011B&0
ParentIdPrefix:  7&1f5848f3&0
Driver:{4D36E967-E325-11CE-BFC1-08002BE10318}\0035
Disk  Stamp: 09/06/2010 15:18
Volume Stamp: 09/06/2010 15:18 
(3) --- Maxtor OneTouch III USB Device 
instanceID: 044303E5&0
ParentIdPrefix:  
Driver:{4D36E967-E325-11CE-BFC1-08002BE10318}\0032
Disk Stamp: 09/10/2010  10:09
Volume Stamp: 03/12/2010 10:42

how can i parse this file so that i can get in following format
Code:
hostname Devic_name instanceID ParentPrefix LastMountedAs Driver 
pcname       kingston         xxxx            xxxxxxxxx        xxxxxxxxxx  xxxxxxxx
pcname       maxtor     0440xxx 4            d 367          08/07/2010  xxxxxxxx
pcname         kingston        xxxxxxx            xxxxxxx         xxxxxxxxx     xxxxxxxx

the pc name will be take from hostname command

the output desirable is in csv format for database with some batch or awk script........
any suggestion in this regard is greatly appreciated
thanks in advance/.........

Last edited by Franklin52; 09-15-2010 at 02:53 AM.. Reason: Please use code tags!
# 2  
Old 09-15-2010
Code:
awk 'BEGIN {"hostname"|getline x;t="?";print "hostname, Device_name, InstanceID, ParentPrefix, LastMountedAs, Driver"}
  $0 ~ "\) ---"       {a=$3}
  /instanceID/        {b=$NF}
  /ParentIdPrefix:/   {c=$2}
  /Last Mounted As:/  {d=$NF}
  /Driver/            {y=FS;FS=":";e=$NF;FS=y;printf("%s, %s, %s, %s, %s, %s\n", x,a?a:t,b?b:t,c?c:t,d?d:t,e?e:t);a=b=c=d=e=""}
' file

# 3  
Old 09-16-2010
thank u its working correctly ..but i want a slight change in the script!! how to add 2 more columns it to it.........

that is !!! diskstamp and volumestamp

hostname Devic_name instance ParentPrefix Lamtdv Driver diskstamp volumestamp
pcname kingston xxxx xxxxxxxxx xxxxxxxxxx xxxxxxxx pcname maxtor 0440xxx 4 d 367 08/07/2010 xxxxxxxx pcname kingston xxxxxxx xxxxxxx xxxxxxxxx xxxxxxxxthanks in advance!~!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to Preprocess a text file and convert into csv

Hello, I was working with Machine learning and would like to apply my regression algorithms on binary classification datasets. So I came across this adult dataset, LIBSVM Data: Classification (Binary Class) It is a binary dataset, features have values only 1 and 0. and I wanted to... (4 Replies)
Discussion started by: ajayram
4 Replies

2. Shell Programming and Scripting

Trying extract from text file and convert csv

I want to extract IP address, system ID and engine IDs of this file ( marked in red) and put in a csv. E.g. 1.1.1.1, SYSTEMID, 000012345678981123548912 I get these file by running an expect script from solaris. Here is the text file output of my expect script. working on 1.1.1.1 SNMP... (5 Replies)
Discussion started by: pbshillong
5 Replies

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

4. Shell Programming and Scripting

Need a unix script to convert date into Julian format in a text file

The 6th & 7th column of the text files represents date & time. I need this to be converted in julian format using command "date +%s -d <date>". I know the command, but dont know how to use it on the script 0 dbclstr-b IXT_Web Memphis_Prod_SQL_Full Memphis-Prod-SQL-Full-Application-Backup... (4 Replies)
Discussion started by: ajiwww
4 Replies

5. Shell Programming and Scripting

Format text to bold from perl script to csv

Hi everyone, is there any way in perl using which we can print the selective words in bold when we write the output to a csv file? Please find the example below 1. Filename: A 2. name age 12 3. city add 23 Line1 should only be bold. Outputs from other files being read in the... (2 Replies)
Discussion started by: ramakanth_burra
2 Replies

6. Shell Programming and Scripting

Convert the below file to csv format

Hi , i want to change this question, i will post soon.. (6 Replies)
Discussion started by: srikanth2567
6 Replies

7. Shell Programming and Scripting

Is there any script which convert binary file to CSV format

Dear guys; I have a binary file and I need to convert its data to csv format ...appreciating your help. Best Regards (14 Replies)
Discussion started by: ahmad.diab
14 Replies

8. Programming

convert text file to csv

hi all, i have a select query that gives me the output in the following way... SYSTYPE -------------------------------------------------------------------------------- Success Failures Total RFT ---------- ---------- ---------- ---------- TYP 1 0 ... (3 Replies)
Discussion started by: sais
3 Replies

9. UNIX for Dummies Questions & Answers

Unix script to convert .csv file to.xls format

I have a .csv file in Unix box i need a UNIX script to convert the.csv files to.xls format. Its very urgent please help me. (1 Reply)
Discussion started by: moon_friend
1 Replies

10. Shell Programming and Scripting

Convert a csv file to an xls format

Hi, I have a file coming in xxx.txt(csv format) i do some work on it and i need to send out as a .xls format. Is there any way there is some code i can use in my script to convert this? I'm struggling on this. Thanks (11 Replies)
Discussion started by: Pablo_beezo
11 Replies
Login or Register to Ask a Question