awk not giving the output expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk not giving the output expected
# 1  
Old 11-12-2018
awk not giving the output expected

Hello,

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

i/p data
Code:
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

Code:
#!/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
Code:
c1,c2,c3,c4
1,a,b,mismatch
1,b,b,match

but all I am getting is
Code:
c1,c2,c3
1,a,b
1,b,b

or if just say awk'{print $NF}
I am getting the output as
Code:
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

Code:
#!/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

Code:
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.

Last edited by RudiC; 11-12-2018 at 03:57 PM..
# 2  
Old 11-12-2018
Quote:
Originally Posted by 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:
Code:
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

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-12-2018
Hi try something like this:
Code:
awk '{print $0, ($NF==$(NF-1))?"match":"mismatch"}' FS=, OFS=, filename.txt

# 4  
Old 11-12-2018
Quote:
Originally Posted by 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 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:
Code:
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 ------

Quote:
Originally Posted by Scrutinizer
Hi try something like this:
Code:
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
Code:
#!/bin/bash
awk -F, '{print $0, NR==1?"c4":($NF==$(NF-1))?"match":"mismatch"}' OFS=,

Ideone.com - oarShB - Online Bash Interpreter & Debugging Tool
# 5  
Old 11-12-2018
Quote:
Originally Posted by 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.




Quote:
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.
# 6  
Old 11-12-2018
Quote:
Originally Posted by 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop not giving expected output

#cat /tmp/input old_array old_dev new_dev new_array 0577 008AB 01744 0125 0577 008AC 01745 0125 0577 008AD 005C8 0125 0577 008AE 005C9 0125 0577 008AF 005CA 0125 0577 008B0 005CB 0125 0577 008B1 005CC 0125 cat test.sh #!/bin/ksh... (4 Replies)
Discussion started by: mbak
4 Replies

2. Shell Programming and Scripting

awk output not what was expected

Good Moring, I am currently reading about awk in a manual and following the examples using the oratab file. My system is SOLARIS 10 I think I am getting strange behavior judging by what the book says to do and what I am getting with my little program. Here is my program: grep -v oratab |... (4 Replies)
Discussion started by: bdby
4 Replies

3. UNIX for Dummies Questions & Answers

Grep not giving expected results

Version: RHEL 5.8 I am doing a grep of the piped output from ps command as shown below. I am grepping for the pattern ora_dbw* . But, in the result set I am seeing strings with ora_dbr* as well like ora_dbrm_SDLM1DAS3 as shown below. Any idea why is this happening ? $ ps -ef | grep... (6 Replies)
Discussion started by: John K
6 Replies

4. AIX

lsrsrc not giving any output

Hi I am getting below output when I run lsrsrc IBM.ManagementServer sbkpshrasd02# lsrsrc "IBM.ManagementServer" Resource Persistent Attributes for IBM.ManagementServer I started the ctcas service but still no use. Stopped and started RMC. Any ideas what needs to be done. ----------... (1 Reply)
Discussion started by: wibhore
1 Replies

5. Programming

Test program not giving expected result

I have five classes. 2 composition classes,1 aggregation class and 1 dependency class.I have coded all the classes but one of my test program is not giving me the expected result.I have the following classes: TimeStamp Interval (composition of 2 TimeStamps) TimeSheet ( aggregation of many... (3 Replies)
Discussion started by: moraks007
3 Replies

6. Solaris

uptime and who giving improper or no output

Hello Everyone, One of our servers is showing a strange issue, let me paste the output root # uptime 4:37pm 3 users, load average: 0.00, 0.04, 0.04 Its been running since months but you can see after time there isn't any output like up 191 days(s). Even the who command with b... (1 Reply)
Discussion started by: vishalaswani
1 Replies

7. Shell Programming and Scripting

awk not generating the expected output

Hi, I am presently stuck in a csv file. INPUT CSV baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played EXPECTED OUTPUT CSV ... (7 Replies)
Discussion started by: scripter12
7 Replies

8. Shell Programming and Scripting

AWK not giving me correct output.

i have a line like this in my script IP=`get_IP <hostname> | awk '{ print $1 }' echo $IP the problem is get_IP <hostname> returns data formated as follows: ip 1.1.1.1 name server_name the code above returns 1.1.1.1 server_name and i just need the 1.1.1.1 I have tried to add "|... (5 Replies)
Discussion started by: mcdef
5 Replies

9. Shell Programming and Scripting

tr command giving wrong output

Hi All, i have a file which have many fields delimited by ,(comma) now i have to show only few fields and not all. the sample text file looks like this: TYPE=SERVICEEVENT, TIMESTAMP=05/06/2009 11:01:40 PM, HOST=sppwa634, APPLICATION=ASComp, FUNCTION=LimitsService, SOU... (8 Replies)
Discussion started by: usha rao
8 Replies

10. Shell Programming and Scripting

script not giving the desired output

Hi, I have a script in which an entry like this ..... FILENAME_B="PIC_${DATE}0732*.JPG" The script connects to an ATM and pull a pic file from it.The format for the file is like PIC_2008061400000001.JPG in the ATM. Means 1st 8 digit is the date(YYYYMMDD) field 2nd 8 digit means hrs... (2 Replies)
Discussion started by: Renjesh
2 Replies
Login or Register to Ask a Question