Sponsored Content
Top Forums Shell Programming and Scripting awk not giving the output expected Post 303025817 by mkathi on Monday 12th of November 2018 03:59:37 PM
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
 

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 05:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy