This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 5 years ago.
How to print nth column in a file using awk?To print the second column of file, I try:
#!/bin/bash
awk '{print $2}' file
But if n is a variable, how to print nth column of file using awk?
#!/bin/bash
n=2
n=2
awk -v var="$n" '{print $var}' file
Give a try this, notice the -v option:
#!/bin/bash
n=3
awk -v x=${n} '{print $x}' file
From the man page:
The option -v followed by var=value is an assignment to be done before
prog is executed; any number of -v options may be present.
For more examples, you could check Using Shell Variables in Programs
Related
This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 4 years ago.
I am listing the files/directories which are greather than N days using the below commands
DATE=`date +%Y-%m-%d`
dt=`date --date "$dt" +%Y%m%d`
loop_dt=`date -I --date "$dt -1 day"`
*** output of loop_dt = 2018-02-25***
hdfs dfs -ls r /path/ | awk '$6 < "$loop_dt"'
I know the above hdfs command is wrong, But I want to pass the loop_dt varible in awk command, to know the list the files which are older than n days
Note: if I hardcode the date in awk command I am getting the results
To recap what I have said in the comments you need to fix your awk command in the following way:
$ cat file
2015-08-01
2015-08-13
$ awk -v var="2015-08-12" '{if( $1 < var"") print}' file
2015-08-01
Replace 2015-08-12 by your shell variable "$loop_dt" and it should work.
Explanations:
Use this syntax awk -v awkVarName="$shellVariable" 'BEGIN {print awkVarName}' to pass a variable to awk
In if( $1 < var"") the "" force the string comparison.
This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 6 years ago.
I want to add a word at the end of each line in my text file which is stored in variable. whenever i execute shell script instead of concatenate content stored in variable variable itself get concatenated. Below is the example for same:
Input:
cat output2.txt
12345
att1=Ramesh^Mumbai
awk '{print $0"^$att1"}' output2.txt >output3.txt
output:
12345^att1
Desired Output:
12345^Ramesh^Mumbai
Try this:
awk -v att1='Ramesh^Mumbai' -v OFS='^' '{print $0,att1}'
-v option allows to pass variable to awk
OFS is the output field separator (that will replace the , in the print statement by ^)
man awk:
-v var=val
Assign the value val to the variable var, before execution of
the program begins. Such variable values are available to the
BEGIN block of an AWK program.
you can use this;
#!/bin/bash
att1=Ramesh^Mumbai
awk -v att1=$att1 '{print $0"^"att1}' output.txt > output3.txt
Example;
user#host:/tmp$ cat output.txt
12345
abab
dafadf
adfaf
user#host:/tmp$, ./test.sh
user#host:/tmp$ cat output3.txt
12345^Ramesh^Mumbai
abab^Ramesh^Mumbai
dafadf^Ramesh^Mumbai
adfaf^Ramesh^Mumbai
This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 6 years ago.
I am trying to match a shell variable with the second column separated by commas.
So I've set $INOW to ls and the command looks like this
awk -F"," '$2 == $INOW {print "yeah it's there"}' commands.csv
I have tried putting quotes around $INOW. no difference. How do i make a match to the shell variable?
You cant use shell variables directly in awk. Rather you can create an awk variable using -v
Example
$ a_variable=hello
$ awk -v var="$a_variable" "{print var}"
hello
So in your case you can write like,
$ awk -F"," -v inow="$INOW" '$2 == inow {print "yeah its there"}' commands.csv
awk -F"," '{if($2 == '$INOW') {print "yeah its there"}else{print "nope"}}' commands.csv
This question already has answers here:
Using awk with variables
(3 answers)
Closed 8 years ago.
I am trying to write a shell script to get certain data from below sample logs..
Below is a sample log:
2014-07-08 16:08:25,684: |ABC_130|1|10123ffffff2|P|489440201
2014-07-08 17:08:25,684: |ABC_130|1|aaaaaxxxxaab|P|489440201
2014-07-08 19:08:25,684: |ABC_130|1|aaaaababbaab|P|489440201
Below is a part of the script where I am facing issue, the issue I am facing is that the awk command doesn't give any output.
#!/bin/sh
DATE_HOUR="`date -d '1 hour ago' "+%Y-%m-%d %H"`"
awk -F ":" '{if ($1='"$DATE_HOUR"') print $0}' log.txt
Don't use shell variable like that in awk. Use -v name=val:
awk -F ":" -v dt="$DATE_HOUR" '$1==dt' log.txt
btw I reduced your awk command to '$1==dt' since print $0 is default action and also if condition can be moved out of curly braces.
This question already has answers here:
Can grep show only words that match search pattern?
(15 answers)
Closed 8 years ago.
I have a bash file that has below line along with other lines.
var BUILD_VERSION = '2014.17.10_23';
I just want to extract 2014.17.10_23 and this value may change so something like grep for 2014* . However when I do that I get the whole line returned instead of the value 2014.17.10_23.
What would be the best way to achieve this?
Thanks
Using awk:
awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]"
And with sed:
sed -n "/BUILD_VERSION/s/.*'\([^']*\)'.*/\1/p" input
grep 'BUILD_VERSION' <your file> | sed -e 's/var BUILD_VERSION = //g'
Would get you '2014.17.10_23'; tweak the sed expression (or pipe it through a few more) to get rid of quotes.
It would be a 1 liner regex in Perl...
Here is another awk solution:
awk -F' = ' '/BUILD_VERSION/ {gsub(/\x27|;/,""); print $NF}'
You can use this awk
awk -F\' '/BUILD_VERSION/ {print $2}' file
2014.17.10_23