How to match to a shell variable in awk? [duplicate] - linux

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

Related

Why when I use a variable in the command does it stop working? || Shell Scripting [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 1 year ago.
good morning, I am a newbie in the world of scripts and I have this problem and I don't know why it happens to me and why I have it wrong, thanks in advance.
For example, I have this command that searches for users with X less letters:
cut -d: -f1 /etc/passwd | awk 'length($1) <= 4'
It works correctly but when I substitute a 4 for a variable with the same value it doesn't do it well:
number=4
echo -e $(cut -d: -f1 /etc/passwd | awk 'length($1) <= $number')
The same error happens to me here too, when I search for users who have an old password
awk -F: '{if($3<=18388)print$1}' < /etc/shadow
Works, but when I use the variable it stops working
variable=18388
awk -F: '{if($3<=$variable)print$1}' < /etc/shadow
Consider using awk's ability to import variables via the -v option, eg:
number=4
cut -d: -f1 /etc/passwd | awk -v num="${number}" 'length($1) <= num'
Though if the first field from /etc/passwd contains white space, and you want to consider the length of the entire field, you should replace $1 with $0, eg:
number=4
cut -d: -f1 /etc/passwd | awk -v num="${number}" 'length($0) <= num'
Even better, eliminate cut and the subprocess (due to the pipe) and use awk for the entire operation by designating the input field separator as a colon:
number=4
awk -F':' -v num="${number}" 'length($1) <= num { print $1 }' /etc/passwd
You need to use double quotes:
echo -e $(cut -d: -f1 /etc/passwd | awk "length(\$1) <= $number")
In single quotes, variables are not interpreted.
Updated: Here is an illustrative example:
> X=1; echo '$X'; echo "$X"
$X
1
Update 2: as rightfully pointed out in the comment, when using double quotes one need to make sure that the variables that are meant for awk to interpret are escaped, so they are not interpreted during script evaluation. Command updated above.
Alternatively, this task could be done using only a single GNU sed one-liner:
num=4
sed -E "s/:.*//; /..{$num}/d" /etc/passwd
Another one-liner, using only grep would be
grep -Po "^[^:]{1,$num}(?=:)" /etc/passwd
but this requires a grep supporting perl regular expressions, for the lookahead construct (?=...).

how to print nth column in a file using awk [duplicate]

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

Using Variable in awk command [duplicate]

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

Not able to get any output from awk command in shell script [duplicate]

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.

How to grep for specific pattern in a file [duplicate]

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

Resources