Find the answer to your Linux question:
Results 1 to 5 of 5
This is all in ksh. I am trying to extract variables from stdout and assign them to an array. So I have a block of text: Code: Display (ABC) Test ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    14

    Grep/awk text for keyword

    This is all in ksh.

    I am trying to extract variables from stdout and assign them to an array.

    So I have a block of text:
    Code:
    Display (ABC)  Test
    Display (XYZ)  Test2
    What I want to do is grab 'ABC' and 'XYZ' and put them in an array.

    So far I am using <command> | grep Display

    I seem to remember that there is an easy way to do this with awk. Any suggestions?

  2. #2
    Just Joined!
    Join Date
    Feb 2006
    Posts
    7
    U can just try out with this, if this is within a text file

    cat x | grep ABC | awk '{ print $Num }'

    where x-stands for filename, ABC-stands for string or word u want to grep and $Num-stands for "ABC" word count in a file.

    OR

    grep ABC or XYZ filename | awk '{ print $Num }'

    where ABC or XYZ -stands for the word u wanted to grep, filename-stnads for file which contains that word and Num-stands for ABC or XYZ word count

    By
    Basu

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by basavaraj View Post
    U can just try out with this, if this is within a text file

    cat x | grep ABC | awk '{ print $Num }'

    where x-stands for filename, ABC-stands for string or word u want to grep and $Num-stands for "ABC" word count in a file.
    I'm not sure I understand you - does Num contain the field number (set in the calling shell)? If so, the single quotes around the awk argument will prevent it from being substituted.

    I'd use sed for this problem:
    Code:
    sed -n 's/Display.*(\([^)]*\)).*/\1/p' filename
    I'll leave you to figure out how to load them into an array.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by penguino888 View Post
    This is all in ksh.

    I am trying to extract variables from stdout and assign them to an array.

    So I have a block of text:
    Code:
    Display (ABC)  Test
    Display (XYZ)  Test2
    What I want to do is grab 'ABC' and 'XYZ' and put them in an array.

    So far I am using <command> | grep Display

    I seem to remember that there is an easy way to do this with awk. Any suggestions?
    depending on what you want to do with those variables, you can do processing inside awk itself
    Code:
    # awk 'BEGIN{FS="[()]"}{print $2}' file
    ABC
    XYZ
    or if you want to export those variables out to your shell script
    Code:
    # result=$(awk 'BEGIN{FS="[()]"}{print $2}' file)
    # echo $result
    ABC XYZ
    # set -- $result
    # echo $1
    ABC
    # echo $2
    XYZ

  5. #5
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    awk also has the split() function which will process your text into an array and return the number of elements.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...