simple value with variable replacement in awk doesn't work [duplicate] - awk

This question already has answers here:
Awk with a variable
(2 answers)
Closed 6 years ago.
Having this line
Doc=$(awk '/1516001/ { print substr($0,15,11) }' /home/data.txt)
want to change the 1616001 with a variable.
for example:
Var='1516001'
Doc=$(awk '/$Var/ { print substr($0,15,11) }' /home/data.txt)
But it doesn't work

#Pedro, in awk a variable's value doesn't work like shell's variable, so we have to assign shell variable's value to an awk's variable and then use it.
Doc=$(awk -vvar="$Var" '{if($0 ~ var){print substr($0,15,11)} }' /home/data.txt)
Let me know if this helps you.

You can use awk -v
Var='1516001'
Doc=$(awk -v pat="$Var" ' $0~pat{ print substr($0,15,11) }' /home/data.txt)

Related

why is awk not writing variable to csv file? [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 1 year ago.
I have a csv file that I need to add a number of columns at the end. The new columns are variables taken from other files.
STNO=3389
STNNAME=SWORDS
awk -F "," '{ stnname='"$STNNAME"';stno='"$STNO"';print $0","stnname","stno }' infile
example of the output.
992501062,-6.278983000,202105210736,,3389
The stno is written fine but the stnname is blank. It seems like I can use numeric variables but not text.
any help appreciated.
thanks.
You are interpolating the literal symbol SWORDS where apparently you were hoping to interpolate a quoted string. Awk has no variable named SWORDS so it resolves to an empty string.
Better hygiene and stability altogether is to avoid interpolating junk right into your Awk script. Use -v on the Awk command line to pass in values.
awk -v stnname="$STNNAME" -v stno="$STNO" 'BEGIN {FS=OFS=","}
{ print $0, stnname , stno }' infile
Tangentially, avoid upper case for your private shell variables.
It is very easy to get lost in a sea of quotes. Maybe catch the env variables using -v like this:
awk -v stnname="$STNNAME" -v stno="$STNO" -F "," '{ print $0,stnname,stno }' infile
then you can use them in the command directly without trying to piece together a string

how can I read an argument within gsub [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 2 years ago.
I want to write a script which replaces 'gene' feature from the 3rd column of the $1 file into 'quant'.
#!/bin/bash
awk -F "\t" '{gsub("gene","quant",$3);print}' $1
The code works well, however I would like to read "gene" as an argument, so how can I specify argument $2 instead of 'gene' in the above code?
Thanks!
Use -v awkvar="$value" to create an awk variable with a given value. Thus:
#!/bin/bash
awk -v orig="$2" -F '\t' '{gsub(orig,"quant",$3);print}' "$1"

getting specific value using awk command in linux [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 4 years ago.
I have a following file.
cat test.txt
NE|East
OR|East
WB|East
HP|North
HR|North
JK|North
NR|North
PB|North
I have a variable circle which stores the following value.
circle="JK"
Now, I want the value matching my variable. I have used the following code, but it doesn't provide me any output. However, when I manually writes "JK", it shows me the desired result.
awk -F '|' '{if($1==$circle) print $2;}' test.txt
awk -F '|' '{if($1 == "JK") print $2;}' test.txt
North
Please suggest. Help is much appreciated.
Could you please try following.
val="$JK"
awk -v var="$val" -F'|' '$1==var{print $2}' Input_file

user input inside awk -- or -- variable usage in search pattern [duplicate]

This question already has answers here:
get the user input in awk
(3 answers)
Closed 6 years ago.
I'm trying to take a user input in a pure [g]awk code. now the requirement is that I want the user to enter either today or current - number of days to generate a report. I can't find any routine inside the awk to read user's input. sometime back I had read a document on awk where it was done using either sprintf or printf, but I dont know how.
OR
in awk, I'm using BEGIN block to setup a variable and then search based on that, but not finding it quite helpful to search the variable based search. something like below:
awk -F "|" ' BEGIN { PWR="Nov 3"; }
/Deployment started at PWR/ { print $1 + $NF }' /var/log/deployments
this offensively denies me any search for the pattern of "Deployment started at Nov 3".
Inside the regex slashes, you don't have access to your variables. What you can do is make a string out of the search phrase then apply that string as a regex.
awk -F "|" ' BEGIN { PWR="Nov 3"; }
$0 ~ "Deployment started at "PWR { print $1 + $NF }' /var/log/deployments

How to specify case insensitivity in AWK within for loop? [duplicate]

This question already has answers here:
ignorecase in AWK
(4 answers)
Closed 8 years ago.
I have an awk line :
awk -F'/|//' '{for(i=1;i<=NF;i++) if($i=="CUST")break;print $(i)}'
I want the CUST to be case insensitive and I am using ($i==CUST) because the file contains words like CUSTOMER, which should not get matched.
I tried using Character class like: if($i=="[Cc][Uu][Ss][Tt]") but that throws an error.
Your mistake is you are doing string comparison with == when the regular expression comparison operator is ~ and your regular expression string should be like /^[Cc][Uu][Ss][Tt]$/ (notice the anchors ^ and $ stop overmatching):
awk -F'/|//' '{for (i=1;i<=NF;i++) if ($i ~ /^[Cc][Uu[Ss][tT]$/)break; print $i}'
Better approachs would be to use the IGNORECASE variable or the tolower, toupper functions.
Use awk
toupper($i)
or
tolower($i)
Like this:
awk -F'/|//' '{for (i=1;i<=NF;i++) if (tolower($i) == "cust")break; print $i}'