Find the answer to your Linux question:
Results 1 to 3 of 3
Hi everybody, some help is needed before I crash my head. I try to run a ksh script including: echo $(awk -v var=$MyVariable -F '#' '$2 ~ /var/ {print var, ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2

    awk using variable as pattern

    Hi everybody,

    some help is needed before I crash my head. I try to run a ksh script including:

    echo $(awk -v var=$MyVariable -F '#' '$2 ~ /var/ {print var, $2 } $MyFile)

    what I get is:

    /var/ is constant "var" and not content of $MyVariable
    but what I print with var is content of $MyVariable

    any suggestion?

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Try replacing the awk line with

    awk -v var=$MyVariable -F '#' '$2 ~ /var/ {print var, $2 } $MyFile

    Code:
    awk -v var=$MyVariable -F '#' '$2 ~ /'$MyVariable'/ {print var, $2 }' $MyFile

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2
    It works. Thanks a lot for your help.

Posting Permissions

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