Sponsored Content
Top Forums Shell Programming and Scripting awk not giving the output expected Post 303025807 by mkathi on Monday 12th of November 2018 02:13:26 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 01:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy