Results 1 to 10 of 10

Thread: Any way to use this awk command with shell variables?

  1. #1
    Join Date
    Dec 2007
    Location
    Science Station Hermes
    Beans
    611
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Any way to use this awk command with shell variables?

    I came across this awk command for printing the contents of a text file between two strings:

    Code:
    awk '/START/{flag=1;next} /END/{flag=0} flag {print}' file
    But I can't figure out how to make it work with a shell variable. Actually, one variable as part of two different strings, 'name="${Dir}"' and '<!--END${Dir}-->'. A sed command does essentially the same thing, the actual command I'm using being

    Code:
    sed -e "1,/name=\"${Dir}\"/d" -e "/<.--END${Dir}-->/,\$d"
    but this doesn't work if the 'start' line if the first line of the file. I have no idea why.
    Quote Originally Posted by Legendary_Bibo View Post
    I tried, and by tried I mean I did a half a**ed google search, and by half a**ed google search I mean I typed "eread pdg"

  2. #2
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Any way to use this awk command with shell variables?

    You can build the awk command in stages. Example:
    Code:
    #!/bin/sh
    
    # awk '/START/{flag=1;next} /END/{flag=0} flag {print}' input1.txt 
    
    Dir="/this/is/dir"
    
    #name="/this/is/dir"
    #<!--END/this/is/dir-->
    
    pat1="name=\"${Dir}\""
    pat2="<!--END${Dir}-->"
    
    echo pat1=$pat1
    echo pat2=$pat2
    
    # escape the forward slashes in patterns
    pat1p=$(echo $pat1 | sed 's?/?\\/?g')
    pat2p=$(echo $pat2 | sed 's?/?\\/?g')
    echo pat1p=$pat1p
    echo pat2p=$pat2p
    
    awktext="/${pat1p}/{flag=1;next} /${pat2p}/{flag=0} flag {print}"
    
    echo awktext=$awktext
    
    awk "$awktext" input2.txt

  3. #3
    Join Date
    Dec 2007
    Location
    Science Station Hermes
    Beans
    611
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Any way to use this awk command with shell variables?

    Thanks, it worked prefectly. I didn't know you could use variables in that way.
    Quote Originally Posted by Legendary_Bibo View Post
    I tried, and by tried I mean I did a half a**ed google search, and by half a**ed google search I mean I typed "eread pdg"

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Any way to use this awk command with shell variables?

    the "awk" way to pass shell variable to awk is using its -v option

    Code:
    shellvar="something"
    awk -v var="$shellvar" '$0 ~ var{print}' file
    this way, there is no messy single/double quotes and your commands look neater. Add more -v if required.

  5. #5
    Join Date
    Dec 2007
    Location
    Science Station Hermes
    Beans
    611
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Any way to use this awk command with shell variables?

    Quote Originally Posted by ghostdog74 View Post
    the "awk" way to pass shell variable to awk is using its -v option

    Code:
    shellvar="something"
    awk -v var="$shellvar" '$0 ~ var{print}' file
    this way, there is no messy single/double quotes and your commands look neater. Add more -v if required.
    I tried that method and also something like this:

    Code:
    awk '{print v1, v2}' v1=$VAR1 v2=$VAR2 input_file
    but couldn't get either of them to work.
    Quote Originally Posted by Legendary_Bibo View Post
    I tried, and by tried I mean I did a half a**ed google search, and by half a**ed google search I mean I typed "eread pdg"

  6. #6
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Any way to use this awk command with shell variables?

    its -v, ie
    Code:
    awk  ..... -v v1="$var1"
    look at the awk usage man page (or the one in my sig) for correct syntax!

  7. #7
    Join Date
    Dec 2007
    Location
    Science Station Hermes
    Beans
    611
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Any way to use this awk command with shell variables?

    Quote Originally Posted by ghostdog74 View Post
    its -v, ie
    Code:
    awk  ..... -v v1="$var1"
    look at the awk usage man page (or the one in my sig) for correct syntax!
    Code:
    $ awk '/ex2/{flag=1;next} /old/{flag=0} flag {print}' file
    index3.html
    index.html
    la
    makepage0.sh
    makepage2.sh
    makepage.sh
    $ A=ex2;B=old
    $ awk -v v1="$A" -v v2="$B" '/v1/{flag=1;next} /v2/{flag=0} flag {print}' file
    $ awk -v v1=ex2 -v v2=old '/v1/{flag=1;next} /v2/{flag=0} flag {print}' file
    $ awk '/v1/{flag=1;next} /v2/{flag=0} flag {print}' file -v v1="$A" -v v2="$B"
    awk: cannot open -v (No such file or directory)
    $ awk '/v1/{flag=1;next} /v2/{flag=0} flag {print}' -v v1="$A" -v v2="$B" file
    awk: cannot open -v (No such file or directory)
    $ awk '/v1/{flag=1;next} /v2/{flag=0} flag {print}' v1="$A" v2="$B" file
    $
    I tried it all again in case I had done something wrong, but again no workey. The variables at the end method came from tek-tips.
    Last edited by alphaniner; August 2nd, 2010 at 02:44 PM.
    Quote Originally Posted by Legendary_Bibo View Post
    I tried, and by tried I mean I did a half a**ed google search, and by half a**ed google search I mean I typed "eread pdg"

  8. #8
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Any way to use this awk command with shell variables?

    Quote Originally Posted by alphaniner View Post
    Code:
    $ awk '/ex2/{flag=1;next} /old/{flag=0} flag {print}' file
    index3.html
    index.html
    la
    makepage0.sh
    makepage2.sh
    makepage.sh
    $ A=ex2;B=old
    $ awk -v v1="$A" -v v2="$B" '/v1/{flag=1;next} /v2/{flag=0} flag {print}' file
    $ awk -v v1=ex2 -v v2=old '/v1/{flag=1;next} /v2/{flag=0} flag {print}' file
    $ awk '/v1/{flag=1;next} /v2/{flag=0} flag {print}' file -v v1="$A" -v v2="$B"
    awk: cannot open -v (No such file or directory)
    $ awk '/v1/{flag=1;next} /v2/{flag=0} flag {print}' -v v1="$A" -v v2="$B" file
    awk: cannot open -v (No such file or directory)
    $ awk '/v1/{flag=1;next} /v2/{flag=0} flag {print}' v1="$A" v2="$B" file
    $
    I tried it all again in case I had done something wrong, but again no workey. The variables at the end method came from tek-tips.
    when you pass in a variable from shell, and when you have /v1/, this tells awk to find the string "v1" , not the variable v1. To search for string stored in variable v1, you use ~

    Code:
    awk '$0 ~ v1 { print "..." }' file

  9. #9
    Join Date
    Dec 2007
    Location
    Science Station Hermes
    Beans
    611
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Any way to use this awk command with shell variables?

    Quote Originally Posted by ghostdog74 View Post
    when you pass in a variable from shell, and when you have /v1/, this tells awk to find the string "v1" , not the variable v1. To search for string stored in variable v1, you use ~

    Code:
    awk '$0 ~ v1 { print "..." }' file
    I think I'm starting to get it now. The following works:
    Code:
    awk -v v1="$A" -v v2="$B" '$0 ~ v1{flag=1;next} $1 ~ v2{flag=0} flag {print}' file
    Is that the proper way to do it? In any case, thanks for the help!
    Quote Originally Posted by Legendary_Bibo View Post
    I tried, and by tried I mean I did a half a**ed google search, and by half a**ed google search I mean I typed "eread pdg"

  10. #10
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Any way to use this awk command with shell variables?

    Quote Originally Posted by alphaniner View Post
    Is that the proper way to do it?
    yes.

Bookmarks

Posting Permissions

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