# awk not giving the output expected

**URL:** <https://community.unix.com/t/awk-not-giving-the-output-expected/374120>\
**Category:** Shell Programming and Scripting\
**Created:** [November 12, 2018, 2:13pm UTC](https://community.unix.com/t/awk-not-giving-the-output-expected/374120 "2018-11-12T14:13:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkathi](https://community.unix.com/letter_avatar/mkathi/32/6_449c533f586939b6e5f246632a46421f.png) [@mkathi](https://community.unix.com/u/mkathi)\
**Post date:** [November 12, 2018, 2:13pm UTC](https://community.unix.com/t/awk-not-giving-the-output-expected/374120/1 "2018-11-12T14:13:26Z")

</div>

Hello,

I am practising awk and decided to compare two columns and print the result of the comparison as third column

i/p data

```nohighlight
c1,c2,c3
1,a,b
1,b,b

```

i am trying to compare the last two columns and if they match I am trying to print `match` else `mismatch` (Ideally i want that as a last column in the same file

```nohighlight
#!/bin/bash
IFS="," while read -r $line
a=awk '{print $1, ($NF=$(NF-1))?"match":"mismatch"}'<filename.txt
awk -v var=$a '{BEGIN s=a}{print $0,s}
'

```

expected output

```nohighlight
c1,c2,c3,c4
1,a,b,mismatch
1,b,b,match

```

but all I am getting is

```nohighlight
c1,c2,c3
1,a,b
1,b,b

```

or if just say `awk'{print $NF}`  
I am getting the output as

```nohighlight
c1,c2,c3
1,a,b
1,b,b

```

same out put even if i use $(NF-1)

I also tried commenting the  
`awk -v var=$a '{BEGIN s=a}{print $0,s}` line in my code and ran it

```nohighlight
#!/bin/bash
IFS="," while read -r $line
awk '{print $1, ($NF=$(NF-1))?"match":"mismatch"}'<filename.txt
#awk -v var=$a '{BEGIN s=a}{print $0,s}
'

```

the output i get is

```nohighlight
c1,c2,c3,match
1,a,b,match
1,b,b,match

```

But i `Read $NF` returns the last column and `NF-1` returns the last but one column.

Please advice where my thinking is wrong.

Thanks.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 12, 2018, 3:08pm UTC](https://community.unix.com/t/awk-not-giving-the-output-expected/374120/2 "2018-11-12T15:08:21Z")

</div>

> [@mkathi](#):
>
> . . .  
> Please advice where my thinking is wrong.  
> . . .

You're taking the scenic route, running multiple `awk` multiple unneeded times. On top:

- You're reading a line (from where? terminal, I presume?) to no avail - the variable is not used anywhere any more.
- You're assigning a variable ` a ` to no avail innthe first `awk` .
- You're using that ` a ` variable as a constant, independent of the condition you want applied, in the second `awk` .
- You're using the assignment operator ` = ` in lieu of the comparison operator `==` .
- You're not giving `awk` the right field separator ` , ` .

Try instead:

```plaintext
awk -F, '{print $0, NR==1?"c4":($2!=$3?"mis":"")"match"}' OFS=, file
c1,c2,c3,c4
1,a,b,mismatch
1,b,b,match

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [November 12, 2018, 3:12pm UTC](https://community.unix.com/t/awk-not-giving-the-output-expected/374120/3 "2018-11-12T15:12:54Z")

</div>

Hi try something like this:

```nohighlight
awk '{print $0, ($NF==$(NF-1))?"match":"mismatch"}' FS=, OFS=, filename.txt

```

---

<div class="post-metadata">

**Author:** ![mkathi](https://community.unix.com/letter_avatar/mkathi/32/6_449c533f586939b6e5f246632a46421f.png) [@mkathi](https://community.unix.com/u/mkathi)\
**Post date:** [November 12, 2018, 3:59pm UTC](https://community.unix.com/t/awk-not-giving-the-output-expected/374120/4 "2018-11-12T15:59:37Z")

</div>

> [@rudic](#):
>
> You're taking the scenic route, running multiple `awk` multiple unneeded times. On top:
> 
> - You're reading a line (from where? terminal, I presume?) to no avail - the variable is not used anywhere any more.
> 
> I am running my code in [ideone.com](http://ideone.com) so Instead of filename i am passing input from stdin.
> 
> - You're assigning a variable ` a ` to no avail innthe first `awk` .
> 
> I was trying to assign the output to variable a ie matching or mismatch
> 
> - You're using that ` a ` variable as a constant, independent of the condition you want applied, in the second `awk` .
> 
> - You're using the assignment operator ` = ` in lieu of the comparison operator `==` .
> 
> yes i see = is to assign the value and == is comparison thanks.
> 
> - You're not giving `awk` the right field separator ` , ` .
> 
> do you mean IFS="," or i should also specify OFS when writing back to file.
> 
> Try instead:
> 
> ```plaintext
> awk -F, '{print $0, NR==1?"c4":($2!=$3?"mis":"")"match"}' OFS=, file
> c1,c2,c3,c4
> 1,a,b,mismatch
> 1,b,b,match
> 
> ```

what's the meaning of : after "c4" in the above code written does this represent start of if condition also  
-- ignore this i understood that you are checking if its the first line then add a field called c4 else compare the two columns and print the result in the end.

inside the if condition  
($2!=$3?"mis":"")"match" i have only two possibilities match and mismatch so would i be wrong to write  
($2!=$3?"mismatch":"match")

thank-you

------ Post updated at 08:59 PM ------

> [@scrutinizer](#):
>
> Hi try something like this:
> 
> ```plaintext
> awk '{print $0, ($NF==$(NF-1))?"match":"mismatch"}' FS=, OFS=, filename.txt
> 
> ```

Yes it works but this code wont add a field to my file and print the values in it I have used the code mentioned by RUDIC and this iworks

```plaintext
#!/bin/bash
awk -F, '{print $0, NR==1?"c4":($NF==$(NF-1))?"match":"mismatch"}' OFS=,

```

[Ideone.com - oarShB - Online Bash Interpreter & Debugging Tool](https://ideone.com/oarShB)

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 12, 2018, 4:41pm UTC](https://community.unix.com/t/awk-not-giving-the-output-expected/374120/5 "2018-11-12T16:41:28Z")

</div>

> [@mkathi](#):
>
> so would i be wrong to write `($2!=$3?"mismatch":"match")`

No. Using `($2!=$3?"mis":"")"match"` is just me being lazy avoiding typing in "match" twice when it's printed anyhow, in either case.

> [@](#):
>
> Yes it works but this code wont add a field to my file

In fact, it does. Unfortunately, it adds "mismatch" in lieu of "c4" to the header line.

---

<div class="post-metadata">

**Author:** ![mkathi](https://community.unix.com/letter_avatar/mkathi/32/6_449c533f586939b6e5f246632a46421f.png) [@mkathi](https://community.unix.com/u/mkathi)\
**Post date:** [November 12, 2018, 6:25pm UTC](https://community.unix.com/t/awk-not-giving-the-output-expected/374120/6 "2018-11-12T18:25:18Z")

</div>

> [@rudic](#):
>
> No. Using `($2!=$3?"mis":"")"match"` is just me being lazy avoiding typing in "match" twice when it's printed anyhow, in either case.
> 
> I get it thanks
> 
> In fact, it does. Unfortunately, it adds "mismatch" in lieu of "c4" to the header line.

Okay i will once again look at it thanks
