why does my awk code that should print word only on specific condition actually prints for all lines? - awk

I am trying to learn awk functionalities and as a simple exercise I try to print values in a file where if the first word is PERMNO then it should print the third word else it should just ignore.
the code I am using is
awk '{if ($1 = "PERMNO"){ print $3}}' ddoutput.txt
Right now this prints third word from every line. But I expect it to print only the third word when the first word of line is PERMNO. What am I missing?

With $1 = "PERMNO" you're assigning PERMNO to first field, which always evaluates to true. You should use the == operator like:
awk '{if($1=="PERMNO"){print $3}}' file
Or more awkish:
awk '$1=="PERMNO"{print $3}' file

Related

Using awk to print the word on the second column of the second line of a file only if it ends in `.local`?

I'd like to print the word on the second column of the second line of a file, only if it ends in .local.
How can I achieve this using awk?
Right now I have awk 'FNR==2{print $2}', but this prints the word no matter what.
Adding a check for $2 against RE:
awk 'FNR == 2 && $2 ~ /\.local$/ { print $2 }'

awk to parse field if specific value is in another

In the awk below I am trying to parse $2 using the _ only if $3 is a specific valus (ID). I am reading that parsed value into an array and going to use it as a key in a lookup. The awk does execute but the entire line 2 or line with ID in $3 prints not just the desired. The print statement is only to see what results (for testing only) and will not be part of the script. Thank you :).
awk
awk -F'\t' '$3=="ID"
f="$(echo $2|cut -d_ -f1,1)"
{
print $f
}' file
file tab-delimited
R_Index locus type
17 chr20:31022959 NON
18 chr11:118353210-chr9:20354877_KMT2A-MLLT3.K8M9 ID
desired
$f = chr11:118353210-chr9:20354877
Not completely clear, could you please try following.
awk '{split($2,array,"_");if(array[2]=="KMT2A-MLLT3.K8M9"){print array[1]}}' Input_file
Or if you want top change 2nd field's value along with printing all lines then try following once.
awk '{split($2,array,"_");if(array[2]=="KMT2A-MLLT3.K8M9"){$2=array[1]}} 1' Input_file

AWK - get value between two strings over multiple lines

input.txt:
>block1
111111111111111111111
>block2
222222222222222222222
>block3
333333333333333333333
AWK command:
awk '/>block2.*>/' input.txt
Expected output
222222222222222222222
However, AWK is returning nothing. What am I misunderstanding?
Thanks!
If you want to print the line after the line containing >block2, then you could use:
awk '/^>block2$/ { nr=NR+1 } NR == nr { print }'
Track the record number plus 1 when you find the match; when the current record number matches the remembered one, print the current record.
If you want all the lines between the line >block2 and >block3, then you'd use:
awk '/^>block2$/,/^>block3/ {if ($0 !~ /^>block[23]$/) print }'
For all lines between the two markers, if the line doesn't match either marker, print it. The output is the same with the sample data file.
another awk
$ awk 'c&&c--; /^>block2/{c=1}' file
222222222222222222222
c specifies how many lines you want to print after the match. If you want the text between two markers
$ awk '/^>block3/{exit} s; /^>block2/{s=1}' file
222222222222222222222
if there are multiple instances and you want them all, just change exit to s=0
You probably meant:
$ awk '/>/{f=/^>block2$/;next} f' file
222222222222222222222

awk all lines of the input file where first and the second field are the same

I am having time here with the following task. I need to print all lines of the input file
where 1st field matches the 2nd. Here is my syntax, which apparently is not working:
awk '$1==$2 {print $0}' < inputfile, any ideas what is wrong ?
The third field would be $3, no?
awk '$1==$3' inputfile
(Since we're here, you could delete the print $0, which is implied, and also the < redirection.)

Assigning a variable on each line in a quick awk command

Awk is awesome of text manipulation, but a little opaque to me. I would like to run an awk command that boils down to something like this
awk '{$x = ($3 > 0 ? 1 : -1); print $1*$x "\t" $2*$x}' file
I want to assign $x on each line, i.e. not using the -v option, and then use it inside my print statement. Unforunately, after the ; awk has forgotten the values of $1 and $2. And putting the assignment outside the braces doesn't seem to work either. How does this work?
AWK doesn't use dollar signs on its variables:
awk '{x = ($3 > 0 ? 1 : -1); print $1*x "\t" $2*x}' file
In your version you're assigning a 1 or -1 to $0 (the entire input line) since x==0 (effectively) when the first line of the input file is read. That's why $1 and $2 seem to be "forgotten".