#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Csv file's line into array help


    Hi,

    I need in SunOS bash to parse csv file and work with column values. I have a problem with no-trimmed column values. How can I get rid of the blanks ?

    example of csv file:
    line1 col_val_1 |line1 col_val 2
    line2 col_val_1 |line2 col_val 2

    output I have now :
    ;line1 col_val_1 ;
    ;line2 col_val_1 ;

    output I want to have:
    ;line1 col_val_1;
    ;line2 col_val_1;

    Is it possible to use sed 's/[ ]*\|/\|/g' before reading lines is started ?
    Or better way

    Thanks

    Code:
    ...
    while read line; do
     IFS="|"
     set $line
    
     column_1=$1
     column_2=$2
    
     # do not want to use own trim function here one every column
     # because parsing is too slow
    
     echo ";${column_1};"
    
    done < $FILE
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Why not use nawk for the while thing?
    (not tested!)
    Code:
    nawk -F\| '{printf(";%s;",$1)}' $FILE
    You may need to change the ;s to %;
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo