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

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Echo $<value> should return a single line for each FOR LOOP iteration


    - test.csv file

    ONE, TWO, THREE
    FOUR, FIVE, SIX
    SEVEN, EIGHT, NINE

    - CODE

    echo "FIELD1, FIELD2, FIELD3" > test_cpy.csv
    for exm in `cat test.csv|grep ,`
    do
    V1=`echo $exm | awk -F", " '{print $1}'`
    V2=`echo $exm | awk -F", " '{print $2}'`
    V3=`echo $exm | awk -F", " '{print $3}'`
    echo "$V1, $V2, $V3" >>test_cpy.csv
    done

    - DESIRED OUTPUT

    FIELD1, FIELD2, FIELD3
    ONE, TWO, THREE
    FOUR, FIVE, SIX
    SEVEN, EIGHT, NINE

    - ACTUAL OUTPUT I AM GETTING

    FIELD1, FIELD2, FIELD3
    ONE,
    TWO,
    THREE,
    FOUR,
    FIVE,
    SIX,
    SEVEN,
    EIGHT,
    NINE

    Hello guys, can some one please suggest how I can modify the code to get output in single line for each iteration.
    I tried "tr" command in echo but that produces the output of all iterations of the FOR loop in a single line.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    225
    Rep Power
    41
    If you want to use awk look at this info to not have awk add carriage return to end of variable:
    http://stackoverflow.com/questions/2...inting-newline
    You could also change your script to not have to invoke awk, see this info:
    http://www.reddit.com/r/commandline/...e_line_in_csv/
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,400
    Rep Power
    1688
    Aren't you really over-egging the problem? Unless I am mistaken all you are doing is prepending a header to an existing file.
    Surely a simple:
    Code:
    echo "FIELD1, FIELD2, FIELD3" > test_cpy.csv
    cat test.csv >> test_cpy.csv
    would do what you wish?
    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