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

    Join Date
    May 2007
    Posts
    3
    Rep Power
    0

    How to parse file and assign parsed contents to variables?


    .ksh newbie

    Could some please point me to some good help tools on how I can read in a file, and parse out content to assign to variables?

    Contents of Example file (misc_data.dat):
    warehouse,"/this/that"
    home,"/this2/that"
    node,"good"

    Basically, i want to read in the file and assign "this/that", "this2/that", and "good" to different variables. Is this possible?
    Last edited by bdg8255; May 29th, 2007 at 09:56 AM. Reason: revise
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,448
    Rep Power
    1751
    Yes, very easily!

    Try:
    Code:
    awk -F\, '{print $2}' your_input_file
    When you are happy it is producing what you want, then:
    Code:
    myvar=$(awk -F\, '{print $2}' your_input_file)
    That will, of course, put ALL the output into one variable.
    If you know what the 'first bit' of theline is going to be you can do
    Code:
    mywharehouse=$(grep "^warehouse" your_input_file |awk -F\, '{print $2}')
    or something similar
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    177
    Rep Power
    234
    in bash
    Code:
    #!/bin/sh
    IFS=,
    while read var1 var2
    do
    echo "$var1 , $var2" 
    done < "file"
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2007
    Posts
    3
    Rep Power
    0

    Thank you so much!


    I really appreciate the help.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    50
    Rep Power
    10
    Originally Posted by ghostdog74
    in bash
    Code:
    #!/bin/sh
    IFS=,
    while read var1 var2
    do
    echo "$var1 , $var2" 
    done < "file"
    Code:
    #!/bin/ksh
    
    while IFS=, read var1 var2
    do
    echo "$var1 , $var2" 
    done < "file"
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    2
    Rep Power
    0
    I saw this thread from a search engine and registered on this website JUST because this solved a problem that I had been having for 3 days now, Thank you thank you thank you

IMN logo majestic logo threadwatch logo seochat tools logo