Parse variable length status,timestamp CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse variable length status,timestamp CSV
# 1  
Old 02-27-2012
Parse variable length status,timestamp CSV

I have the output of a process which on status change of the object being processed appends status,timestamp to the record in the text file...

so i end up with output something like:

Code:
 
task123,TERMINAL,glob,5,INITIAL,2012-02-27 16:48:07,PREPARING,2012-02-27 16:49:06,SCHEDULED,2012-02-27 16:49:06,TRANSLATING,2012-02-27 16:49:07,TERMINAL,2012-02-27 16:50:52
task345,COMPLETE,glob2,6,INITIAL,2012-02-27 16:48:07,PREPARING,2012-02-27 16:49:06,SCHEDULED,2012-02-27 16:49:07,TRANSLATING,2012-02-27 16:49:22,LOADING,2012-02-27 16:50:53,COMPLETE,2012-02-27 16:50:53

what i'd like to do is parse this into a set of variables for a given job (i'll end up looping through the file record by record)...

what i'm not sure is how to get the value after a matched string and save it in a variable...

so for example i want to save the timestamp following "PREPARING" into a variable called v_PREPARING

and i want to iterate through the line to get all of these values... the good thing i guess (though not sure how its helpful) is that the value after glob/glob2 is a count of the number of status' on that line...

thoughts?
# 2  
Old 02-27-2012
Try this:

Code:
while IFS=, read job status glob cnt rest
do
    eval $(echo $rest | awk -F, '{for(i=1;i<NF;i+=2)print "v_"$i"=\""$(i+1)"\""}')
 
    # Your code goes here....
    echo "Task: " $job
    echo "Status: " $status
    echo "INITIAL: " $v_INITIAL
 
    #Unset all vars for next loop around
    eval $(echo $rest | awk -F, '{for(i=1;i<NF;i+=2)print "unset v_"$i}')
done < infile


Last edited by Chubler_XL; 02-27-2012 at 08:04 PM..
# 3  
Old 02-27-2012
I'd avoid the evals if I could...

Code:
#!/bin/bash

IFS=","
while read LINE
do
        set -- $LINE

        TERMINAL=
        INITIAL=
        PREPARING=
        SCHEDULED=
        TRANSLATING=

        TASK=$1
        shift        

        while [ "$#" -gt 0]
        do
                VAR=$1
                shift
                read $VAR <<<"$1"
        done
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

2. Shell Programming and Scripting

Check/Parse log file's lines using time difference/timestamp

I was looking at this script which outputs the two lines which differs less than one sec. #!/usr/bin/perl -w use strict; use warnings; use Time::Local; use constant SEC_MILIC => 1000; my $file='infile'; ## Open for reading argument file. open my $fh, "<", $file or die "Cannot... (1 Reply)
Discussion started by: cele_82
1 Replies

3. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

4. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

5. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. Shell Programming and Scripting

Parse a line which has different word length

Hi All, Please let me know a command to parse the below line and find the words, I have a line like this 40609 39930 In this above line the two words are separted by space.The length of this two words may differ. I want to put 40609 in var_one and 39930 in var_two. Eg. Input line is ... (1 Reply)
Discussion started by: girish.raos
1 Replies

7. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

8. Shell Programming and Scripting

use awk to read variable length csv

Any help to read the contents of a variable length csv ....??(using awk) The csv mite look like this : anjali,ram,rahul,mohini,sam,.... and so on ... I need to pick up each name.. Thanks in advance SD (3 Replies)
Discussion started by: shweta_d
3 Replies

9. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question