awk If expression - Return string if not true


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk If expression - Return string if not true
# 1  
Old 08-19-2015
Code awk If expression - Return string if not true

Hi,

I have the following txt file List_With_Duplicates.txt;

Code:
a,1,1
b,3,4
c,5,2
d,6,1
e,3,3
f,3,7

When I run the command
awk -F ',' '{if($2==$3){print $1","$2","$3}}' List_With_Duplicates.txt I get the following output;

Code:
a,1,1
e,3,3

This works! as I've compared the 2nd & 3rd column for duplicates and printed the lines which match. However, say I have the following file List_No_Duplicates.txt;

Code:
b,3,4
c,5,2
d,6,1
f,3,7

This file has no duplicates. When I run the command awk -F ',' '{if($2==$3){print $1","$2","$3} else {print "No Duplicates"}}' List_No_Duplicates.txt I get the following output;

Code:
No Duplicates
No Duplicates
No Duplicates
No Duplicates

However, I just want 1 statement saying No Duplicates rather than printing the same statement for each line.

Its probably really simple explanation but I just cant get anything to work. I've tried using False & True operators and an exit statement but nothing works.

Would appreciate any help with this.
Thanks

Last edited by Don Cragun; 08-19-2015 at 06:51 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-19-2015
You are telling awk to either print the line that contains duplicates or print the string No Duplicates for each input line you read from the file. Try this instead:
Code:
awk '
BEGIN {	FS = OFS = ","
}
$2 == $3 {
	print
	f++
}
END {	if(f == 0) print "No Duplicates"
}' List_No_Duplicates.txt

If someone wants to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 3  
Old 08-19-2015
Thanks for the quick reply.

When I run this on a txt file with no duplicates it works great. But when I run it on a file with duplicates I get the following output;

Code:
 
 awk 'BEGIN { FS = OFS = "," } $2 == $3 { print f++ } END {if(f == 0) print "No Duplicates"}' List_With_Duplicates.txt

Code:
 
 0
 1

Preferred output is;

Code:
 
 a,1,1
 e,3,3

The code seems to output the iteration number of 'f' rather than the value. Is there an amendment that can be made to change this?

Thanks
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multiline sample code, input, and output!


---------- Post updated at 11:41 AM ---------- Previous update was at 11:23 AM ----------

I tried;

Code:
 
 awk 'BEGIN { FS = OFS = "," } $2 == $3 { print f++ $1,$2,$3 } END {if(f == 0) print "No Duplicates"}' List_With_Duplicates.txt

And got the following output;

Code:
 
 0a,1,1
 1e,3,3

Is there a way of hiding the iteration number? When I do the same command but without the f++ the 'Non Duplicate' string is output also.

Thanks
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multiline sample code, input, and output!


---------- Post updated at 11:42 AM ---------- Previous update was at 11:41 AM ----------

Sorry, was using wrong tabs

Last edited by Don Cragun; 08-19-2015 at 07:49 AM.. Reason: Fix tags yet again.
# 4  
Old 08-19-2015
I gave you a working, easy to read and understand multi-line awk script. You converted it into a harder to read, non-working single-line awk script.

If you don't understand awk syntax well enough to convert it correctly, please at least try the code you are given before changing it and complaining that the modifications you made to the code you were given don't work.

I personally prefer multi-line scripts because they are easier to maintain. If you insist on a single line script try:
Code:
awk -F, '$2==$3{print;f++}END{if(!f)print "No Duplicates"}' OFS=',' List_With_Duplicates.txt

# 5  
Old 08-19-2015
Wasn't complaining, just new to this and trying different things.
Appreciate your help
# 6  
Old 08-19-2015
For your problem in post#3, drop the f++ in the print statement. (In fact, you could drop everything from it as an print on its own will print $0, which is the entire line)
# 7  
Old 08-19-2015
I think your main impediment is you need to know how awk script work:

Every awk-script consists of three parts (although each part is optional): the first part is marked with BEGIN and is executed before the first line of the input is read. The second part is not marked with a name and is executed every time a line of input is read. The last part is named END and is executed after the last line of input is read.

For instance:

Code:
awk 'BEGIN  {
               printf( "Here we start.\n" );
            }
            {
               print;
            }
     END    {
               printf( "Here it ends.\n" );
            }' /path/to/input

This will print the input file unaltered (the single "print" command in the middle does that), surrounded by "Here we start" at the top (the effect of the BEGIN clause) and "Here it ends" at the bottom (the effect of the END clause).

Your original script printed "no duplicates" several times because the process - deciding upon printing it or not, then printing it eventually - was done for every line over and over. Dons script worked because he put that into the END-part and just manipulated a flag without printing anything in the main part.

You should be able with this to change his script into doing whatever you want it to do.

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk evaluating a string as a math expression

Hi, I am writing a script in awk trying to replace strings that are math expressions with their result. For example, I have a file that looks like this: 5-1 32/8-1 4*12 17+1-3 I would like to get the following output: 4 3 48 15 I tried doing it the following way (using the "bc"... (8 Replies)
Discussion started by: avi.levi
8 Replies

2. Shell Programming and Scripting

String regular expression

Hi, temp="/usr=25,/usr/lib=12" How to get only dir names with out values. I tried like below but no use. tmp=${temp##*,} echo $tmp o/p: /usr/lib=12 expected o/p: /usr /usr/lib ---> in array (13 Replies)
Discussion started by: munna_dude
13 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

How can awk search a string without using regular expression?

Hello, Awk seem treat the pattern as regular expression, how can awk search not using regular expression? e.g. just represent for "", not "A" or "a" . I don't want to add backslash . (2 Replies)
Discussion started by: 915086731
2 Replies

5. Shell Programming and Scripting

Grep with regulare expression to find carrige return

Gurus, I have a files from where lines are like following <ns0:ccid>123456789</ns0:ccid> <ns0:ccid>1234 56789</ns0:ccid> I would like to grep any number which will be as below (with carrige return): As 123456789 any number so I have to use the regular expression <ns0:ccid>1234... (3 Replies)
Discussion started by: thepurple
3 Replies

6. Shell Programming and Scripting

How to return a string?

function blah { return "string" } it keeps saying string: not found How can i do this guys? Because I'm trying to do something like this function print_daemon_options { echo "Start Daemons - Please enter one or a combination of the following:" if isDatasubEnabled &&... (11 Replies)
Discussion started by: pyscho
11 Replies

7. UNIX for Dummies Questions & Answers

Proper Expression To Not Include A String...

I have a folder of scripts: bash:/folderpath/> ls beginFile.sh beginFileBackup.sh beginAnother.sh beginAnotherBackup.sh beginJunk.sh beginJunkBackup.sh I'd like to be able to call just one (beginFile.sh) using this type of scheme: #Run the beginFile script without the word "Backup" in... (1 Reply)
Discussion started by: mrwatkin
1 Replies

8. Shell Programming and Scripting

validate a string against a regular expression

Hi there i have a script which will create unix user accounts. Id like to validate the entered string so that it is specifically 8 characters or less and consists of only ! not Is there a way to validate a string against a regular expression.. i.e size=`printf "$var | wc -m` ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

9. Shell Programming and Scripting

return string in functions

Hi friends I need to return string value in functions can anyone help me out to return string values rather than integer. #!/bin/bash add_a_user() { USER=$1 COMPANY=$2 shift; shift; echo "Adding user $USER ..." echo "$USER working in $COMPANY ..." ret_type=YES return... (1 Reply)
Discussion started by: kittusri9
1 Replies

10. Shell Programming and Scripting

deleting 'carriage return' from string

hi there I'm getting a string from a sqlplus query, and I need to compare it with another string Problem here, is that the string i get from query brings a 'carriage return' with it, and comparing throws always false value. i need to delete all carriage retun charactres in string. how... (6 Replies)
Discussion started by: viko
6 Replies
Login or Register to Ask a Question