getting specific value using awk command in linux [duplicate] - awk

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

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 to separate columns in linux using awk or cat [duplicate]

This question already has answers here:
bash: shortest way to get n-th column of output
(8 answers)
Closed 2 years ago.
I have a file with 900.000 columns, the structure is:
1613 1200000012000500000011111.......
112345 1200000012000500000011111.......
1287659 1200000012000500000011111.......
1234 1200000012000500000011111.......
712826 1200000012000500000011111.......
I need only the numbers before the space, this is a new file as:
1613
112345
1287659
1234
712826
I try with
cat -df.txt |cut -d |,| -f7
but it does not work.
A couple of approaches, based on your attempts:
awk
As also suggested by RavinderSingh13, straightforward awk approach is
awk '{print $1}' yourfile.txt
remember awk doesn't need cat.
cut
With vanilla cut as well, this should work:
cut -f1 -d' ' yourfile.txt
Here you require cut to print the first field -f1 where the delimiter is a whitespace -d' '. Remember also cut doesn't need cat (unlike me always forgetting).
Other very nice approaches with grep and sed can be found in this question

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"

How to find the total of second column using awk commands?

Input file(filename:cat)
item1,200
item2,499
item3,699
item4,800
Awk command which i had tried
awk -F"," '{x+=$2}END{print x}'cat
Error
The above command display empty output.Is it any possible way to overcome with any solutions for it.
Edited and Final command
awk -F"," '{x+=$2}END{print x}' cat

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

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)