Register
It is currently Wed Nov 26, 2014 7:47 am

text to csv conversion


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Jun 03, 2011 2:56 am   

Joined: Fri Jun 03, 2011 2:15 am
Posts: 1
Thank u every body ......just need a help so that a text file needs to be converted into CSV.............

my log file is as follows

Host scsi3: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 09:01:21 GMT 2011

Host scsi3: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 11:22:14 GMT 2011

Host scsi4: usb-storage
Vendor: Maxtor
Product: OneTouch III
Serial Number: 044303E5
Protocol: Transparent SCSI
Transport: Bulk
Quirks:
Tue May 31 11:29:44 GMT 2011




Now how can i convert this to csv file so that i can export it into some database

for example:-

scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks
usb-storage, Maxtor, OneTouchIII, 044303E5, Transparent SCSI, Bulk, Tue May 31 09:01:21 GMT 2011
usb-storage, Maxtor, OneTouchIII, 044303E5, Transparent SCSI, Bulk, Tue May 31 11:22:14 GMT 2011
usb-storage, Maxtor, OneTouchIII, 044303E5, Transparent SCSI, Bulk, Tue May 31 11:29:44 GMT 2011

after some googling i got a clue about getline command with awk....... but dont know how to use;............

any suggestions in this regards greatly appreciated
regards


the orinal file is uploaded for reference...


You do not have the required permissions to view the files attached to this post.


Top
 Profile  
 PostPosted: Fri Jun 03, 2011 7:57 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Not really to your full spec, but can get you started:
Code:
#! /bin/bash

first=1

while read line ; do
    if [[ -z $line ]] ; then
        if ((first)) ; then
            (IFS=, ; echo "${header[*]}")
            first=0
        fi
        (IFS=, ; echo "${out[*]}")
        out=()
    else
        if [[ $line == *[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\ GMT\ [0-9]* ]] ; then
            out+=("$line")
        else
            h=${line%:*}
            t=${line#*:}
            t=${t# }
            if ((first)) ; then
                header+=("$h")
            fi
            out+=("$t")
        fi
    fi
done


Top
 Profile  
 PostPosted: Wed Jun 08, 2011 9:07 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Hello.

Another approach (it respects your specifications) :

Code:
#!/bin/bash

if (($# != 1)) || [[ ! -f $1 ]]; then
    printf 'E : one file has to be passed as argument.\n' >&2
    exit 1
fi

printf 'scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks\n'

while read -r; do
    case "$REPLY" in
        '')
            printf '\n'
            i=0
            read -r
            ;; 
   
        *:)
            read -r
    esac

    if ((i++)); then
        printf ', '
    fi 

    printf %s "${REPLY#*:[[:blank:]]}"
done < "$1"

# if the file is not newline terminated
if ((i)); then
    printf '\n'
fi


Though, it's faster with awk(1) (like 10ms less) and you don't have to check whether the file is newline terminated or not.
Here is the code :

Code:
#!/usr/bin/awk -f

BEGIN \
{
    FS = ": "
   
    print "scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks"
}

{
    while ($0 ~ /^.*:$/) {
        getline nl
        printf(", %s", nl)
        next
    }   

    while (! NF) {
       print
       i = 0
       next
    }   

    if (i++)
        printf(", ")
   
    printf("%s", $2)
}

END \
{
    if (i)
        printf("\n")
}


You might want the « quick awk version », so here it is :

Code:
awk -F ': ' 'BEGIN {print "scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks"} /^.*:$/ {getline nl; printf(", %s", nl); next} ! NF {print; i = 0; next} i++ {printf(", ")} {printf("%s", $2)} END {if (i) printf("\n")}' <file>


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP