linux - [Solved-5 Solutions] How to use shell variables in an awk script in Linux ? - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to use shell variables in an awk script in Linux ?

Linux - Solution 1:

Here 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 one\nline two"
awk -v var="$variable" 'BEGIN {print var}'
line one
line two
click below button to copy the code. By - Linux tutorial - team

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

variable="line one\nline two"
awk 'BEGIN {print "'"$variable"'"}'
line one
line two
click below button to copy the code. By - Linux tutorial - team

Here is an example of code injection:

variable='line one\nline two" ; for (i=1;i<=1000;++i) print i"'
awk 'BEGIN {print "'"$variable"'"}'
line one
line two
1
2
3
.
.
1000
click below button to copy the code. By - Linux tutorial - team

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

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 one\nline two"
echo "input data" | awk '{print var}' var="$variable"
or
awk '{print var}' var="$variable" file
click below button to copy the code. By - Linux tutorial - team

Variable can also be added to awk using here string

awk '{print $0}' <<< "$variable"
test
click below button to copy the code. By - Linux tutorial - team

This is the same as:

echo "$variable" | awk '{print $0}'
click below button to copy the code. By - Linux tutorial - team

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
click below button to copy the code. By - Linux tutorial - team

Other errors you can get without double quote:

variable="line one\nline two"
awk -v var=$variable 'BEGIN {print var}'
awk: cmd. line:1: one\nline
awk: cmd. line:1:    ^ backslash not last character on line
awk: cmd. line:1: one\nline
awk: cmd. line:1:    ^ syntax error
click below button to copy the code. By - Linux tutorial - team

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

awk -v var='$variable' 'BEGIN {print var}'
$variable
click below button to copy the code. By - Linux tutorial - team

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
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

It seems that the good-old ENVIRON awk built-in hash is not mentioned at all. For Example:

$ X=Solaris awk 'BEGIN{print ENVIRON["X"], ENVIRON["TERM"]}'
Solaris rxvt
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

Use either of these depending how you want backslashes in the shell variables handled :

awk -v avar="$svar" '... avar ...' file
awk 'BEGIN{avar=ARGV[1];ARGV[1]=""}... avar ...' "$svar" file
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 4:

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
click below button to copy the code. By - Linux tutorial - team

Or to make it clearer:

% environment_variable=123test
% awk -vawk_variable="${environment_variable}" 'BEGIN { print awk_variable }'
123test
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

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
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - How to use shell variables in an awk script in Linux ?