How do I use shell variables in an awk script?












195














I found some ways to pass external shell variables to an awk script, but I'm confused about ' and ".



First, I tried with a shell script:



$ v=123test
$ echo $v
123test
$ echo "$v"
123test


Then tried awk:



$ awk 'BEGIN{print "'$v'"}'
$ 123test
$ awk 'BEGIN{print '"$v"'}'
$ 123


Why is the difference?



Lastly I tried this:



$ awk 'BEGIN{print " '$v' "}'
$ 123test
$ awk 'BEGIN{print ' "$v" '}'
awk: cmd. line:1: BEGIN{print
awk: cmd. line:1: ^ unexpected newline or end of string


I'm confused about this.










share|improve this question




















  • 1




    I like the -v as shown below, but this is really a great exercise in thinking about how to protect things from the shell. Working through this, my first cut use backslashes on spaces and dollar signs. Needless to say the examples here were well worth my time.
    – Chris
    Dec 20 '16 at 21:00










  • Related: Difference between single and double quotes in awk.
    – codeforester
    May 11 at 18:45
















195














I found some ways to pass external shell variables to an awk script, but I'm confused about ' and ".



First, I tried with a shell script:



$ v=123test
$ echo $v
123test
$ echo "$v"
123test


Then tried awk:



$ awk 'BEGIN{print "'$v'"}'
$ 123test
$ awk 'BEGIN{print '"$v"'}'
$ 123


Why is the difference?



Lastly I tried this:



$ awk 'BEGIN{print " '$v' "}'
$ 123test
$ awk 'BEGIN{print ' "$v" '}'
awk: cmd. line:1: BEGIN{print
awk: cmd. line:1: ^ unexpected newline or end of string


I'm confused about this.










share|improve this question




















  • 1




    I like the -v as shown below, but this is really a great exercise in thinking about how to protect things from the shell. Working through this, my first cut use backslashes on spaces and dollar signs. Needless to say the examples here were well worth my time.
    – Chris
    Dec 20 '16 at 21:00










  • Related: Difference between single and double quotes in awk.
    – codeforester
    May 11 at 18:45














195












195








195


63





I found some ways to pass external shell variables to an awk script, but I'm confused about ' and ".



First, I tried with a shell script:



$ v=123test
$ echo $v
123test
$ echo "$v"
123test


Then tried awk:



$ awk 'BEGIN{print "'$v'"}'
$ 123test
$ awk 'BEGIN{print '"$v"'}'
$ 123


Why is the difference?



Lastly I tried this:



$ awk 'BEGIN{print " '$v' "}'
$ 123test
$ awk 'BEGIN{print ' "$v" '}'
awk: cmd. line:1: BEGIN{print
awk: cmd. line:1: ^ unexpected newline or end of string


I'm confused about this.










share|improve this question















I found some ways to pass external shell variables to an awk script, but I'm confused about ' and ".



First, I tried with a shell script:



$ v=123test
$ echo $v
123test
$ echo "$v"
123test


Then tried awk:



$ awk 'BEGIN{print "'$v'"}'
$ 123test
$ awk 'BEGIN{print '"$v"'}'
$ 123


Why is the difference?



Lastly I tried this:



$ awk 'BEGIN{print " '$v' "}'
$ 123test
$ awk 'BEGIN{print ' "$v" '}'
awk: cmd. line:1: BEGIN{print
awk: cmd. line:1: ^ unexpected newline or end of string


I'm confused about this.







bash shell awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 3 at 19:41









codeforester

17.4k83864




17.4k83864










asked Sep 29 '13 at 7:45









hqjma

978275




978275








  • 1




    I like the -v as shown below, but this is really a great exercise in thinking about how to protect things from the shell. Working through this, my first cut use backslashes on spaces and dollar signs. Needless to say the examples here were well worth my time.
    – Chris
    Dec 20 '16 at 21:00










  • Related: Difference between single and double quotes in awk.
    – codeforester
    May 11 at 18:45














  • 1




    I like the -v as shown below, but this is really a great exercise in thinking about how to protect things from the shell. Working through this, my first cut use backslashes on spaces and dollar signs. Needless to say the examples here were well worth my time.
    – Chris
    Dec 20 '16 at 21:00










  • Related: Difference between single and double quotes in awk.
    – codeforester
    May 11 at 18:45








1




1




I like the -v as shown below, but this is really a great exercise in thinking about how to protect things from the shell. Working through this, my first cut use backslashes on spaces and dollar signs. Needless to say the examples here were well worth my time.
– Chris
Dec 20 '16 at 21:00




I like the -v as shown below, but this is really a great exercise in thinking about how to protect things from the shell. Working through this, my first cut use backslashes on spaces and dollar signs. Needless to say the examples here were well worth my time.
– Chris
Dec 20 '16 at 21:00












Related: Difference between single and double quotes in awk.
– codeforester
May 11 at 18:45




Related: Difference between single and double quotes in awk.
– codeforester
May 11 at 18:45












8 Answers
8






active

oldest

votes


















340














Getting shell variables into awk may be done in several ways. Some are better than others.





This is the best way to do it. It uses the -v option: (P.S. use a space after -v or it will be less portable. E.g., awk -v var= not awk -vvar=)



variable="line onenline two"
awk -v var="$variable" 'BEGIN {print var}'
line one
line two


This should be compatible with most awk and variable is available in the BEGIN block as well:



Multiple variables



awk -v a="$var1" -v b="$var2" 'BEGIN {print a,b}'




Here we get the variable after the awk code. This will work fine as long as you do not need the variable in the BEGIN block:



variable="line onenline two"
echo "input data" | awk '{print var}' var="$variable"
or
awk '{print var}' var="$variable" file


This also works with multiple variables
awk '{print a,b,$0}' a="$var1" b="$var2" file





Variable can also be added to awk using here string



awk '{print $0}' <<< "$variable"
test


This is the same as:



echo "$variable" | awk '{print $0}'


PS, this threats the variable as a file input





As TrueY write, you can use the ENVIRON to print Environmental Variables
Setting a variable before running AWK, you can print it out like this:



X=MyVar awk 'BEGIN{print ENVIRON["X"],ENVIRON["SHELL"]}'
MyVar /bin/bash


Edit: As "that other guy" write, this does not handle backslash. Not recommended.





You can use a variable within the awk code, but it's messy and hard to read, and as Charles Duffy points out, this version may also be a victim of code injection. If someone adds bad stuff to the variable, it will be executed as part of the awk code.



If you want to make an awk that changes dynamically with use of variables, you can do it this way, bot DO NOT use it for normal variables.



variable="line onenline two"
awk 'BEGIN {print "'"$variable"'"}'
line one
line two


Here is an example of code injection:



variable='line onenline two" ; for (i=1;i<=1000;++i) print i"'
awk 'BEGIN {print "'"$variable"'"}'
line one
line two
1
2
3
.
.
1000


You can add lots of commands to awk this way. Even make it crash with non valid commands.





It's always good to double quote variable "$variable"

If not, multiple lines will be added as a long single line.



Example:



var="Line one
This is line two"

echo $var
Line one This is line two

echo "$var"
Line one
This is line two


Other errors you can get without double quote:



variable="line onenline two"
awk -v var=$variable 'BEGIN {print var}'
awk: cmd. line:1: onenline
awk: cmd. line:1: ^ backslash not last character on line
awk: cmd. line:1: onenline
awk: cmd. line:1: ^ syntax error


And with single quote, it does not expand the value of the variable:



awk -v var='$variable' 'BEGIN {print var}'
$variable





share|improve this answer























  • "messy and hard to read" ignores the more important security concern of code injection when directly substituting strings into awk code.
    – Charles Duffy
    Feb 18 '16 at 20:21






  • 1




    @CharlesDuffy. You are 100% correct. Cleaned up some more.
    – Jotne
    Feb 18 '16 at 21:07










  • Hi @Jotne, thank you for the answer. However, I wonder if you could explain a little more about how "'"$variable"'" works in awk? It works, but I could not understand it.
    – Ch'en Meng
    Nov 21 '16 at 9:04










  • I am trying with awk '!/master-bhs-01/ ||/172.16./' /etc/hosts to replace master-bhs-01, none of theses synthax worked for me
    – Dimitri Kopriwa
    May 5 '17 at 8:48



















22














It seems that the good-old ENVIRON awk built-in hash is not mentioned at all. An example of its usage:



$ X=Solaris awk 'BEGIN{print ENVIRON["X"], ENVIRON["TERM"]}'
Solaris rxvt





share|improve this answer

















  • 3




    This is a good suggestion because it passes the data verbatim. -v doesn't work when the value contains backslashes.
    – that other guy
    Feb 23 '16 at 21:45






  • 1




    @thatotherguy I did not know that! I thought that if I use awk -v x='cd' ... then it will be used it properly. But when x is printed awk drops the famous: awk: warning: escape sequence 'c' treated as plain 'c' error message... Thanks!
    – TrueY
    Feb 24 '16 at 9:11





















7














Use either of these depending how you want backslashes in the shell variables handled (avar is an awk variable, svar is a shell variable):



awk -v avar="$svar" '... avar ...' file
awk 'BEGIN{avar=ARGV[1];ARGV[1]=""}... avar ...' "$svar" file


See http://cfajohnson.com/shell/cus-faq-2.html#Q24 for details and other options. The first method above is almost always your best option and has the most obvious semantics.






share|improve this answer































    5














    You could pass in the command-line option -v with a variable name (v) and a value (=) of the environment variable ("${v}"):



    % awk -vv="${v}" 'BEGIN { print v }'
    123test


    Or to make it clearer (with far fewer vs):



    % environment_variable=123test
    % awk -vawk_variable="${environment_variable}" 'BEGIN { print awk_variable }'
    123test





    share|improve this answer































      3














      You can utilize ARGV:



      v=123test
      awk 'BEGIN {print ARGV[1]}' "$v"


      Note that if you are going to continue into the body, you will need to adjust
      ARGC:



      awk 'BEGIN {ARGC--} {print ARGV[2], $0}' file "$v"





      share|improve this answer





























        0














        I had to insert date at the beginning of the lines of a log file and it's done like below:



        DATE=$(date +"%Y-%m-%d")
        awk '{ print "'"$DATE"'", $0; }' /path_to_log_file/log_file.log


        It can be redirect to another file to save






        share|improve this answer





















        • The double quote - single quote - double quote was exactly what I needed to make mine work.
          – user53029
          Jul 21 '16 at 14:24










        • This was already mentioned in the accepted answer as a method you should not use due to code injection vulnerabilities. So the information here is redundant (already described in the accepted answer), and incomplete (does not mention the problems with this method).
          – Jason S
          Oct 12 '16 at 5:20



















        0














        I just changed @Jotne's answer for "for loop".



        for i in `seq 11 20`; do host myserver-$i | awk -v i="$i" '{print "myserver-"i" " $4}'; done





        share|improve this answer





























          0














          for i in chr{1..22} chrX chrY
          do
          awk -v chr="$i" '$1==chr' ../snp150.hg19.txt >> $chr.vcf.bed
          echo $i
          done





          share|improve this answer




















            protected by Inian Apr 27 at 19:37



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?














            8 Answers
            8






            active

            oldest

            votes








            8 Answers
            8






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            340














            Getting shell variables into awk may be done in several ways. Some are better than others.





            This is the best way to do it. It uses the -v option: (P.S. use a space after -v or it will be less portable. E.g., awk -v var= not awk -vvar=)



            variable="line onenline two"
            awk -v var="$variable" 'BEGIN {print var}'
            line one
            line two


            This should be compatible with most awk and variable is available in the BEGIN block as well:



            Multiple variables



            awk -v a="$var1" -v b="$var2" 'BEGIN {print a,b}'




            Here we get the variable after the awk code. This will work fine as long as you do not need the variable in the BEGIN block:



            variable="line onenline two"
            echo "input data" | awk '{print var}' var="$variable"
            or
            awk '{print var}' var="$variable" file


            This also works with multiple variables
            awk '{print a,b,$0}' a="$var1" b="$var2" file





            Variable can also be added to awk using here string



            awk '{print $0}' <<< "$variable"
            test


            This is the same as:



            echo "$variable" | awk '{print $0}'


            PS, this threats the variable as a file input





            As TrueY write, you can use the ENVIRON to print Environmental Variables
            Setting a variable before running AWK, you can print it out like this:



            X=MyVar awk 'BEGIN{print ENVIRON["X"],ENVIRON["SHELL"]}'
            MyVar /bin/bash


            Edit: As "that other guy" write, this does not handle backslash. Not recommended.





            You can use a variable within the awk code, but it's messy and hard to read, and as Charles Duffy points out, this version may also be a victim of code injection. If someone adds bad stuff to the variable, it will be executed as part of the awk code.



            If you want to make an awk that changes dynamically with use of variables, you can do it this way, bot DO NOT use it for normal variables.



            variable="line onenline two"
            awk 'BEGIN {print "'"$variable"'"}'
            line one
            line two


            Here is an example of code injection:



            variable='line onenline two" ; for (i=1;i<=1000;++i) print i"'
            awk 'BEGIN {print "'"$variable"'"}'
            line one
            line two
            1
            2
            3
            .
            .
            1000


            You can add lots of commands to awk this way. Even make it crash with non valid commands.





            It's always good to double quote variable "$variable"

            If not, multiple lines will be added as a long single line.



            Example:



            var="Line one
            This is line two"

            echo $var
            Line one This is line two

            echo "$var"
            Line one
            This is line two


            Other errors you can get without double quote:



            variable="line onenline two"
            awk -v var=$variable 'BEGIN {print var}'
            awk: cmd. line:1: onenline
            awk: cmd. line:1: ^ backslash not last character on line
            awk: cmd. line:1: onenline
            awk: cmd. line:1: ^ syntax error


            And with single quote, it does not expand the value of the variable:



            awk -v var='$variable' 'BEGIN {print var}'
            $variable





            share|improve this answer























            • "messy and hard to read" ignores the more important security concern of code injection when directly substituting strings into awk code.
              – Charles Duffy
              Feb 18 '16 at 20:21






            • 1




              @CharlesDuffy. You are 100% correct. Cleaned up some more.
              – Jotne
              Feb 18 '16 at 21:07










            • Hi @Jotne, thank you for the answer. However, I wonder if you could explain a little more about how "'"$variable"'" works in awk? It works, but I could not understand it.
              – Ch'en Meng
              Nov 21 '16 at 9:04










            • I am trying with awk '!/master-bhs-01/ ||/172.16./' /etc/hosts to replace master-bhs-01, none of theses synthax worked for me
              – Dimitri Kopriwa
              May 5 '17 at 8:48
















            340














            Getting shell variables into awk may be done in several ways. Some are better than others.





            This is the best way to do it. It uses the -v option: (P.S. use a space after -v or it will be less portable. E.g., awk -v var= not awk -vvar=)



            variable="line onenline two"
            awk -v var="$variable" 'BEGIN {print var}'
            line one
            line two


            This should be compatible with most awk and variable is available in the BEGIN block as well:



            Multiple variables



            awk -v a="$var1" -v b="$var2" 'BEGIN {print a,b}'




            Here we get the variable after the awk code. This will work fine as long as you do not need the variable in the BEGIN block:



            variable="line onenline two"
            echo "input data" | awk '{print var}' var="$variable"
            or
            awk '{print var}' var="$variable" file


            This also works with multiple variables
            awk '{print a,b,$0}' a="$var1" b="$var2" file





            Variable can also be added to awk using here string



            awk '{print $0}' <<< "$variable"
            test


            This is the same as:



            echo "$variable" | awk '{print $0}'


            PS, this threats the variable as a file input





            As TrueY write, you can use the ENVIRON to print Environmental Variables
            Setting a variable before running AWK, you can print it out like this:



            X=MyVar awk 'BEGIN{print ENVIRON["X"],ENVIRON["SHELL"]}'
            MyVar /bin/bash


            Edit: As "that other guy" write, this does not handle backslash. Not recommended.





            You can use a variable within the awk code, but it's messy and hard to read, and as Charles Duffy points out, this version may also be a victim of code injection. If someone adds bad stuff to the variable, it will be executed as part of the awk code.



            If you want to make an awk that changes dynamically with use of variables, you can do it this way, bot DO NOT use it for normal variables.



            variable="line onenline two"
            awk 'BEGIN {print "'"$variable"'"}'
            line one
            line two


            Here is an example of code injection:



            variable='line onenline two" ; for (i=1;i<=1000;++i) print i"'
            awk 'BEGIN {print "'"$variable"'"}'
            line one
            line two
            1
            2
            3
            .
            .
            1000


            You can add lots of commands to awk this way. Even make it crash with non valid commands.





            It's always good to double quote variable "$variable"

            If not, multiple lines will be added as a long single line.



            Example:



            var="Line one
            This is line two"

            echo $var
            Line one This is line two

            echo "$var"
            Line one
            This is line two


            Other errors you can get without double quote:



            variable="line onenline two"
            awk -v var=$variable 'BEGIN {print var}'
            awk: cmd. line:1: onenline
            awk: cmd. line:1: ^ backslash not last character on line
            awk: cmd. line:1: onenline
            awk: cmd. line:1: ^ syntax error


            And with single quote, it does not expand the value of the variable:



            awk -v var='$variable' 'BEGIN {print var}'
            $variable





            share|improve this answer























            • "messy and hard to read" ignores the more important security concern of code injection when directly substituting strings into awk code.
              – Charles Duffy
              Feb 18 '16 at 20:21






            • 1




              @CharlesDuffy. You are 100% correct. Cleaned up some more.
              – Jotne
              Feb 18 '16 at 21:07










            • Hi @Jotne, thank you for the answer. However, I wonder if you could explain a little more about how "'"$variable"'" works in awk? It works, but I could not understand it.
              – Ch'en Meng
              Nov 21 '16 at 9:04










            • I am trying with awk '!/master-bhs-01/ ||/172.16./' /etc/hosts to replace master-bhs-01, none of theses synthax worked for me
              – Dimitri Kopriwa
              May 5 '17 at 8:48














            340












            340








            340






            Getting shell variables into awk may be done in several ways. Some are better than others.





            This is the best way to do it. It uses the -v option: (P.S. use a space after -v or it will be less portable. E.g., awk -v var= not awk -vvar=)



            variable="line onenline two"
            awk -v var="$variable" 'BEGIN {print var}'
            line one
            line two


            This should be compatible with most awk and variable is available in the BEGIN block as well:



            Multiple variables



            awk -v a="$var1" -v b="$var2" 'BEGIN {print a,b}'




            Here we get the variable after the awk code. This will work fine as long as you do not need the variable in the BEGIN block:



            variable="line onenline two"
            echo "input data" | awk '{print var}' var="$variable"
            or
            awk '{print var}' var="$variable" file


            This also works with multiple variables
            awk '{print a,b,$0}' a="$var1" b="$var2" file





            Variable can also be added to awk using here string



            awk '{print $0}' <<< "$variable"
            test


            This is the same as:



            echo "$variable" | awk '{print $0}'


            PS, this threats the variable as a file input





            As TrueY write, you can use the ENVIRON to print Environmental Variables
            Setting a variable before running AWK, you can print it out like this:



            X=MyVar awk 'BEGIN{print ENVIRON["X"],ENVIRON["SHELL"]}'
            MyVar /bin/bash


            Edit: As "that other guy" write, this does not handle backslash. Not recommended.





            You can use a variable within the awk code, but it's messy and hard to read, and as Charles Duffy points out, this version may also be a victim of code injection. If someone adds bad stuff to the variable, it will be executed as part of the awk code.



            If you want to make an awk that changes dynamically with use of variables, you can do it this way, bot DO NOT use it for normal variables.



            variable="line onenline two"
            awk 'BEGIN {print "'"$variable"'"}'
            line one
            line two


            Here is an example of code injection:



            variable='line onenline two" ; for (i=1;i<=1000;++i) print i"'
            awk 'BEGIN {print "'"$variable"'"}'
            line one
            line two
            1
            2
            3
            .
            .
            1000


            You can add lots of commands to awk this way. Even make it crash with non valid commands.





            It's always good to double quote variable "$variable"

            If not, multiple lines will be added as a long single line.



            Example:



            var="Line one
            This is line two"

            echo $var
            Line one This is line two

            echo "$var"
            Line one
            This is line two


            Other errors you can get without double quote:



            variable="line onenline two"
            awk -v var=$variable 'BEGIN {print var}'
            awk: cmd. line:1: onenline
            awk: cmd. line:1: ^ backslash not last character on line
            awk: cmd. line:1: onenline
            awk: cmd. line:1: ^ syntax error


            And with single quote, it does not expand the value of the variable:



            awk -v var='$variable' 'BEGIN {print var}'
            $variable





            share|improve this answer














            Getting shell variables into awk may be done in several ways. Some are better than others.





            This is the best way to do it. It uses the -v option: (P.S. use a space after -v or it will be less portable. E.g., awk -v var= not awk -vvar=)



            variable="line onenline two"
            awk -v var="$variable" 'BEGIN {print var}'
            line one
            line two


            This should be compatible with most awk and variable is available in the BEGIN block as well:



            Multiple variables



            awk -v a="$var1" -v b="$var2" 'BEGIN {print a,b}'




            Here we get the variable after the awk code. This will work fine as long as you do not need the variable in the BEGIN block:



            variable="line onenline two"
            echo "input data" | awk '{print var}' var="$variable"
            or
            awk '{print var}' var="$variable" file


            This also works with multiple variables
            awk '{print a,b,$0}' a="$var1" b="$var2" file





            Variable can also be added to awk using here string



            awk '{print $0}' <<< "$variable"
            test


            This is the same as:



            echo "$variable" | awk '{print $0}'


            PS, this threats the variable as a file input





            As TrueY write, you can use the ENVIRON to print Environmental Variables
            Setting a variable before running AWK, you can print it out like this:



            X=MyVar awk 'BEGIN{print ENVIRON["X"],ENVIRON["SHELL"]}'
            MyVar /bin/bash


            Edit: As "that other guy" write, this does not handle backslash. Not recommended.





            You can use a variable within the awk code, but it's messy and hard to read, and as Charles Duffy points out, this version may also be a victim of code injection. If someone adds bad stuff to the variable, it will be executed as part of the awk code.



            If you want to make an awk that changes dynamically with use of variables, you can do it this way, bot DO NOT use it for normal variables.



            variable="line onenline two"
            awk 'BEGIN {print "'"$variable"'"}'
            line one
            line two


            Here is an example of code injection:



            variable='line onenline two" ; for (i=1;i<=1000;++i) print i"'
            awk 'BEGIN {print "'"$variable"'"}'
            line one
            line two
            1
            2
            3
            .
            .
            1000


            You can add lots of commands to awk this way. Even make it crash with non valid commands.





            It's always good to double quote variable "$variable"

            If not, multiple lines will be added as a long single line.



            Example:



            var="Line one
            This is line two"

            echo $var
            Line one This is line two

            echo "$var"
            Line one
            This is line two


            Other errors you can get without double quote:



            variable="line onenline two"
            awk -v var=$variable 'BEGIN {print var}'
            awk: cmd. line:1: onenline
            awk: cmd. line:1: ^ backslash not last character on line
            awk: cmd. line:1: onenline
            awk: cmd. line:1: ^ syntax error


            And with single quote, it does not expand the value of the variable:



            awk -v var='$variable' 'BEGIN {print var}'
            $variable






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 24 at 10:10

























            answered Sep 29 '13 at 7:51









            Jotne

            29.2k73244




            29.2k73244












            • "messy and hard to read" ignores the more important security concern of code injection when directly substituting strings into awk code.
              – Charles Duffy
              Feb 18 '16 at 20:21






            • 1




              @CharlesDuffy. You are 100% correct. Cleaned up some more.
              – Jotne
              Feb 18 '16 at 21:07










            • Hi @Jotne, thank you for the answer. However, I wonder if you could explain a little more about how "'"$variable"'" works in awk? It works, but I could not understand it.
              – Ch'en Meng
              Nov 21 '16 at 9:04










            • I am trying with awk '!/master-bhs-01/ ||/172.16./' /etc/hosts to replace master-bhs-01, none of theses synthax worked for me
              – Dimitri Kopriwa
              May 5 '17 at 8:48


















            • "messy and hard to read" ignores the more important security concern of code injection when directly substituting strings into awk code.
              – Charles Duffy
              Feb 18 '16 at 20:21






            • 1




              @CharlesDuffy. You are 100% correct. Cleaned up some more.
              – Jotne
              Feb 18 '16 at 21:07










            • Hi @Jotne, thank you for the answer. However, I wonder if you could explain a little more about how "'"$variable"'" works in awk? It works, but I could not understand it.
              – Ch'en Meng
              Nov 21 '16 at 9:04










            • I am trying with awk '!/master-bhs-01/ ||/172.16./' /etc/hosts to replace master-bhs-01, none of theses synthax worked for me
              – Dimitri Kopriwa
              May 5 '17 at 8:48
















            "messy and hard to read" ignores the more important security concern of code injection when directly substituting strings into awk code.
            – Charles Duffy
            Feb 18 '16 at 20:21




            "messy and hard to read" ignores the more important security concern of code injection when directly substituting strings into awk code.
            – Charles Duffy
            Feb 18 '16 at 20:21




            1




            1




            @CharlesDuffy. You are 100% correct. Cleaned up some more.
            – Jotne
            Feb 18 '16 at 21:07




            @CharlesDuffy. You are 100% correct. Cleaned up some more.
            – Jotne
            Feb 18 '16 at 21:07












            Hi @Jotne, thank you for the answer. However, I wonder if you could explain a little more about how "'"$variable"'" works in awk? It works, but I could not understand it.
            – Ch'en Meng
            Nov 21 '16 at 9:04




            Hi @Jotne, thank you for the answer. However, I wonder if you could explain a little more about how "'"$variable"'" works in awk? It works, but I could not understand it.
            – Ch'en Meng
            Nov 21 '16 at 9:04












            I am trying with awk '!/master-bhs-01/ ||/172.16./' /etc/hosts to replace master-bhs-01, none of theses synthax worked for me
            – Dimitri Kopriwa
            May 5 '17 at 8:48




            I am trying with awk '!/master-bhs-01/ ||/172.16./' /etc/hosts to replace master-bhs-01, none of theses synthax worked for me
            – Dimitri Kopriwa
            May 5 '17 at 8:48













            22














            It seems that the good-old ENVIRON awk built-in hash is not mentioned at all. An example of its usage:



            $ X=Solaris awk 'BEGIN{print ENVIRON["X"], ENVIRON["TERM"]}'
            Solaris rxvt





            share|improve this answer

















            • 3




              This is a good suggestion because it passes the data verbatim. -v doesn't work when the value contains backslashes.
              – that other guy
              Feb 23 '16 at 21:45






            • 1




              @thatotherguy I did not know that! I thought that if I use awk -v x='cd' ... then it will be used it properly. But when x is printed awk drops the famous: awk: warning: escape sequence 'c' treated as plain 'c' error message... Thanks!
              – TrueY
              Feb 24 '16 at 9:11


















            22














            It seems that the good-old ENVIRON awk built-in hash is not mentioned at all. An example of its usage:



            $ X=Solaris awk 'BEGIN{print ENVIRON["X"], ENVIRON["TERM"]}'
            Solaris rxvt





            share|improve this answer

















            • 3




              This is a good suggestion because it passes the data verbatim. -v doesn't work when the value contains backslashes.
              – that other guy
              Feb 23 '16 at 21:45






            • 1




              @thatotherguy I did not know that! I thought that if I use awk -v x='cd' ... then it will be used it properly. But when x is printed awk drops the famous: awk: warning: escape sequence 'c' treated as plain 'c' error message... Thanks!
              – TrueY
              Feb 24 '16 at 9:11
















            22












            22








            22






            It seems that the good-old ENVIRON awk built-in hash is not mentioned at all. An example of its usage:



            $ X=Solaris awk 'BEGIN{print ENVIRON["X"], ENVIRON["TERM"]}'
            Solaris rxvt





            share|improve this answer












            It seems that the good-old ENVIRON awk built-in hash is not mentioned at all. An example of its usage:



            $ X=Solaris awk 'BEGIN{print ENVIRON["X"], ENVIRON["TERM"]}'
            Solaris rxvt






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 6 '14 at 12:46









            TrueY

            5,87412538




            5,87412538








            • 3




              This is a good suggestion because it passes the data verbatim. -v doesn't work when the value contains backslashes.
              – that other guy
              Feb 23 '16 at 21:45






            • 1




              @thatotherguy I did not know that! I thought that if I use awk -v x='cd' ... then it will be used it properly. But when x is printed awk drops the famous: awk: warning: escape sequence 'c' treated as plain 'c' error message... Thanks!
              – TrueY
              Feb 24 '16 at 9:11
















            • 3




              This is a good suggestion because it passes the data verbatim. -v doesn't work when the value contains backslashes.
              – that other guy
              Feb 23 '16 at 21:45






            • 1




              @thatotherguy I did not know that! I thought that if I use awk -v x='cd' ... then it will be used it properly. But when x is printed awk drops the famous: awk: warning: escape sequence 'c' treated as plain 'c' error message... Thanks!
              – TrueY
              Feb 24 '16 at 9:11










            3




            3




            This is a good suggestion because it passes the data verbatim. -v doesn't work when the value contains backslashes.
            – that other guy
            Feb 23 '16 at 21:45




            This is a good suggestion because it passes the data verbatim. -v doesn't work when the value contains backslashes.
            – that other guy
            Feb 23 '16 at 21:45




            1




            1




            @thatotherguy I did not know that! I thought that if I use awk -v x='cd' ... then it will be used it properly. But when x is printed awk drops the famous: awk: warning: escape sequence 'c' treated as plain 'c' error message... Thanks!
            – TrueY
            Feb 24 '16 at 9:11






            @thatotherguy I did not know that! I thought that if I use awk -v x='cd' ... then it will be used it properly. But when x is printed awk drops the famous: awk: warning: escape sequence 'c' treated as plain 'c' error message... Thanks!
            – TrueY
            Feb 24 '16 at 9:11













            7














            Use either of these depending how you want backslashes in the shell variables handled (avar is an awk variable, svar is a shell variable):



            awk -v avar="$svar" '... avar ...' file
            awk 'BEGIN{avar=ARGV[1];ARGV[1]=""}... avar ...' "$svar" file


            See http://cfajohnson.com/shell/cus-faq-2.html#Q24 for details and other options. The first method above is almost always your best option and has the most obvious semantics.






            share|improve this answer




























              7














              Use either of these depending how you want backslashes in the shell variables handled (avar is an awk variable, svar is a shell variable):



              awk -v avar="$svar" '... avar ...' file
              awk 'BEGIN{avar=ARGV[1];ARGV[1]=""}... avar ...' "$svar" file


              See http://cfajohnson.com/shell/cus-faq-2.html#Q24 for details and other options. The first method above is almost always your best option and has the most obvious semantics.






              share|improve this answer


























                7












                7








                7






                Use either of these depending how you want backslashes in the shell variables handled (avar is an awk variable, svar is a shell variable):



                awk -v avar="$svar" '... avar ...' file
                awk 'BEGIN{avar=ARGV[1];ARGV[1]=""}... avar ...' "$svar" file


                See http://cfajohnson.com/shell/cus-faq-2.html#Q24 for details and other options. The first method above is almost always your best option and has the most obvious semantics.






                share|improve this answer














                Use either of these depending how you want backslashes in the shell variables handled (avar is an awk variable, svar is a shell variable):



                awk -v avar="$svar" '... avar ...' file
                awk 'BEGIN{avar=ARGV[1];ARGV[1]=""}... avar ...' "$svar" file


                See http://cfajohnson.com/shell/cus-faq-2.html#Q24 for details and other options. The first method above is almost always your best option and has the most obvious semantics.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 29 '13 at 13:54

























                answered Sep 29 '13 at 13:22









                Ed Morton

                108k124494




                108k124494























                    5














                    You could pass in the command-line option -v with a variable name (v) and a value (=) of the environment variable ("${v}"):



                    % awk -vv="${v}" 'BEGIN { print v }'
                    123test


                    Or to make it clearer (with far fewer vs):



                    % environment_variable=123test
                    % awk -vawk_variable="${environment_variable}" 'BEGIN { print awk_variable }'
                    123test





                    share|improve this answer




























                      5














                      You could pass in the command-line option -v with a variable name (v) and a value (=) of the environment variable ("${v}"):



                      % awk -vv="${v}" 'BEGIN { print v }'
                      123test


                      Or to make it clearer (with far fewer vs):



                      % environment_variable=123test
                      % awk -vawk_variable="${environment_variable}" 'BEGIN { print awk_variable }'
                      123test





                      share|improve this answer


























                        5












                        5








                        5






                        You could pass in the command-line option -v with a variable name (v) and a value (=) of the environment variable ("${v}"):



                        % awk -vv="${v}" 'BEGIN { print v }'
                        123test


                        Or to make it clearer (with far fewer vs):



                        % environment_variable=123test
                        % awk -vawk_variable="${environment_variable}" 'BEGIN { print awk_variable }'
                        123test





                        share|improve this answer














                        You could pass in the command-line option -v with a variable name (v) and a value (=) of the environment variable ("${v}"):



                        % awk -vv="${v}" 'BEGIN { print v }'
                        123test


                        Or to make it clearer (with far fewer vs):



                        % environment_variable=123test
                        % awk -vawk_variable="${environment_variable}" 'BEGIN { print awk_variable }'
                        123test






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Sep 29 '13 at 7:57

























                        answered Sep 29 '13 at 7:49









                        Johnsyweb

                        96.6k18142206




                        96.6k18142206























                            3














                            You can utilize ARGV:



                            v=123test
                            awk 'BEGIN {print ARGV[1]}' "$v"


                            Note that if you are going to continue into the body, you will need to adjust
                            ARGC:



                            awk 'BEGIN {ARGC--} {print ARGV[2], $0}' file "$v"





                            share|improve this answer


























                              3














                              You can utilize ARGV:



                              v=123test
                              awk 'BEGIN {print ARGV[1]}' "$v"


                              Note that if you are going to continue into the body, you will need to adjust
                              ARGC:



                              awk 'BEGIN {ARGC--} {print ARGV[2], $0}' file "$v"





                              share|improve this answer
























                                3












                                3








                                3






                                You can utilize ARGV:



                                v=123test
                                awk 'BEGIN {print ARGV[1]}' "$v"


                                Note that if you are going to continue into the body, you will need to adjust
                                ARGC:



                                awk 'BEGIN {ARGC--} {print ARGV[2], $0}' file "$v"





                                share|improve this answer












                                You can utilize ARGV:



                                v=123test
                                awk 'BEGIN {print ARGV[1]}' "$v"


                                Note that if you are going to continue into the body, you will need to adjust
                                ARGC:



                                awk 'BEGIN {ARGC--} {print ARGV[2], $0}' file "$v"






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jan 15 '17 at 7:05









                                Steven Penny

                                1




                                1























                                    0














                                    I had to insert date at the beginning of the lines of a log file and it's done like below:



                                    DATE=$(date +"%Y-%m-%d")
                                    awk '{ print "'"$DATE"'", $0; }' /path_to_log_file/log_file.log


                                    It can be redirect to another file to save






                                    share|improve this answer





















                                    • The double quote - single quote - double quote was exactly what I needed to make mine work.
                                      – user53029
                                      Jul 21 '16 at 14:24










                                    • This was already mentioned in the accepted answer as a method you should not use due to code injection vulnerabilities. So the information here is redundant (already described in the accepted answer), and incomplete (does not mention the problems with this method).
                                      – Jason S
                                      Oct 12 '16 at 5:20
















                                    0














                                    I had to insert date at the beginning of the lines of a log file and it's done like below:



                                    DATE=$(date +"%Y-%m-%d")
                                    awk '{ print "'"$DATE"'", $0; }' /path_to_log_file/log_file.log


                                    It can be redirect to another file to save






                                    share|improve this answer





















                                    • The double quote - single quote - double quote was exactly what I needed to make mine work.
                                      – user53029
                                      Jul 21 '16 at 14:24










                                    • This was already mentioned in the accepted answer as a method you should not use due to code injection vulnerabilities. So the information here is redundant (already described in the accepted answer), and incomplete (does not mention the problems with this method).
                                      – Jason S
                                      Oct 12 '16 at 5:20














                                    0












                                    0








                                    0






                                    I had to insert date at the beginning of the lines of a log file and it's done like below:



                                    DATE=$(date +"%Y-%m-%d")
                                    awk '{ print "'"$DATE"'", $0; }' /path_to_log_file/log_file.log


                                    It can be redirect to another file to save






                                    share|improve this answer












                                    I had to insert date at the beginning of the lines of a log file and it's done like below:



                                    DATE=$(date +"%Y-%m-%d")
                                    awk '{ print "'"$DATE"'", $0; }' /path_to_log_file/log_file.log


                                    It can be redirect to another file to save







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 2 '16 at 8:07









                                    Sina

                                    30536




                                    30536












                                    • The double quote - single quote - double quote was exactly what I needed to make mine work.
                                      – user53029
                                      Jul 21 '16 at 14:24










                                    • This was already mentioned in the accepted answer as a method you should not use due to code injection vulnerabilities. So the information here is redundant (already described in the accepted answer), and incomplete (does not mention the problems with this method).
                                      – Jason S
                                      Oct 12 '16 at 5:20


















                                    • The double quote - single quote - double quote was exactly what I needed to make mine work.
                                      – user53029
                                      Jul 21 '16 at 14:24










                                    • This was already mentioned in the accepted answer as a method you should not use due to code injection vulnerabilities. So the information here is redundant (already described in the accepted answer), and incomplete (does not mention the problems with this method).
                                      – Jason S
                                      Oct 12 '16 at 5:20
















                                    The double quote - single quote - double quote was exactly what I needed to make mine work.
                                    – user53029
                                    Jul 21 '16 at 14:24




                                    The double quote - single quote - double quote was exactly what I needed to make mine work.
                                    – user53029
                                    Jul 21 '16 at 14:24












                                    This was already mentioned in the accepted answer as a method you should not use due to code injection vulnerabilities. So the information here is redundant (already described in the accepted answer), and incomplete (does not mention the problems with this method).
                                    – Jason S
                                    Oct 12 '16 at 5:20




                                    This was already mentioned in the accepted answer as a method you should not use due to code injection vulnerabilities. So the information here is redundant (already described in the accepted answer), and incomplete (does not mention the problems with this method).
                                    – Jason S
                                    Oct 12 '16 at 5:20











                                    0














                                    I just changed @Jotne's answer for "for loop".



                                    for i in `seq 11 20`; do host myserver-$i | awk -v i="$i" '{print "myserver-"i" " $4}'; done





                                    share|improve this answer


























                                      0














                                      I just changed @Jotne's answer for "for loop".



                                      for i in `seq 11 20`; do host myserver-$i | awk -v i="$i" '{print "myserver-"i" " $4}'; done





                                      share|improve this answer
























                                        0












                                        0








                                        0






                                        I just changed @Jotne's answer for "for loop".



                                        for i in `seq 11 20`; do host myserver-$i | awk -v i="$i" '{print "myserver-"i" " $4}'; done





                                        share|improve this answer












                                        I just changed @Jotne's answer for "for loop".



                                        for i in `seq 11 20`; do host myserver-$i | awk -v i="$i" '{print "myserver-"i" " $4}'; done






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Apr 10 at 7:34









                                        edib

                                        3841513




                                        3841513























                                            0














                                            for i in chr{1..22} chrX chrY
                                            do
                                            awk -v chr="$i" '$1==chr' ../snp150.hg19.txt >> $chr.vcf.bed
                                            echo $i
                                            done





                                            share|improve this answer


























                                              0














                                              for i in chr{1..22} chrX chrY
                                              do
                                              awk -v chr="$i" '$1==chr' ../snp150.hg19.txt >> $chr.vcf.bed
                                              echo $i
                                              done





                                              share|improve this answer
























                                                0












                                                0








                                                0






                                                for i in chr{1..22} chrX chrY
                                                do
                                                awk -v chr="$i" '$1==chr' ../snp150.hg19.txt >> $chr.vcf.bed
                                                echo $i
                                                done





                                                share|improve this answer












                                                for i in chr{1..22} chrX chrY
                                                do
                                                awk -v chr="$i" '$1==chr' ../snp150.hg19.txt >> $chr.vcf.bed
                                                echo $i
                                                done






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jul 20 at 15:13









                                                Shicheng Guo

                                                624714




                                                624714

















                                                    protected by Inian Apr 27 at 19:37



                                                    Thank you for your interest in this question.
                                                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                    Would you like to answer one of these unanswered questions instead?



                                                    Popular posts from this blog

                                                    404 Error Contact Form 7 ajax form submitting

                                                    How to know if a Active Directory user can login interactively

                                                    TypeError: fit_transform() missing 1 required positional argument: 'X'