Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    2

    Question Unanswered: awking the contents of a variable

    hi-
    pardon my noobieness...
    i'm having some trouble with the syntax for awking the contents of of a variable in a script.

    i have a variable (let's say $foo) whose contents is:

    one two three

    how can i assign a new variable ($bar) the value of "three"?

    by comparison, if the variable were instead a file named "foo" with the same contents, then

    awk '/one/{print $3}' foo

    gives me:

    three

    so again, how do i make awk look at the contents of a script variable rather than a file?

    -thanks!
    -rev6

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    You can pipe the string into awk using echo...

    echo $foo | awk '/one/{print $3}'

    or, have awk take its input directly from your variable...

    awk '/one/{print $3}' << !!
    $foo
    !!

    Is this what you mean?

  3. #3
    Join Date
    Sep 2003
    Posts
    2

    Thumbs up thanks!

    thanks- this did the job nicely.
    -rev6



    Originally posted by Damian Ibbotson
    You can pipe the string into awk using echo...

    echo $foo | awk '/one/{print $3}'

    or, have awk take its input directly from your variable...

    awk '/one/{print $3}' << !!
    $foo
    !!

    Is this what you mean?

Posting Permissions

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