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

    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Unix Shell script question


    Hey guys, I've just started with unix, and I have a question. I am trying to pull information from a file and send that information to a variable.

    When I use grep, if gives me the whole line where the info is, that's not what I need, I need the number that comes after the word I am searching for with grep.

    ex. grep dmm textfile.tx
    dmm k k 123456

    Thatīs what I get, I get the dmm I searched for, some spaces then k spaces then k again then some more spaces then the actual number I want to send to a variable.

    How would I go about doing that ? Thanks for everyone who take their time to answer.

    I am using ksh.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    852
    Rep Power
    387

    Cool


    Try:
    Code:
    val=`grep dmm textfile.txt|cut -d' ' -f4`
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,448
    Rep Power
    1751
    Just to show there's a smany different ways of doing things as people you ask ..

    Code:
    val=$(grep "^dmm" textfile.txt| awk '{print $NF})
    The ^ in the grep means start of line, so only lines starting with dmm will be returned (belt and braces!). The awk will return the last field of the line (just in case there are a variable number of fields (space-delimited text) in the line (and piece of string in pocket!)

    Comments on this post

    • aitken325i agrees
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Awesome !


    Guys thank you very much. I ran into another problem, there were two occurrences of the same parameter, but I fixed that with -m 1.



    Take care !
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,448
    Rep Power
    1751
    Good use of the man pages to find the -m operand: not something I have ever used, and I do not recall actually ever seeing it!
    The potential duplication of lines begs a couple of questions:
    a) are the returned lines total duplicates?
    b) is it important which line's data you return if they are not full duplicates?

    You could cut down some of the grief by changing the grep search term to "^dmm " (adding a space) and slipping a sort -u into the pipeline before the awk. The rest is down to application need.
    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