The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
while loop not taking the not equal to condition ssuresh1999 UNIX for Dummies Questions & Answers 9 08-12-2008 10:05 AM
Edit entry if not equal kenshinhimura Shell Programming and Scripting 4 08-07-2008 12:18 AM
Stop+A equal tlee SUN Solaris 5 07-01-2008 07:42 AM
add not equal in script. myguess21 Shell Programming and Scripting 2 02-26-2008 10:20 AM
Making a variable equal a pattern Bab00shka UNIX for Dummies Questions & Answers 3 03-08-2004 06:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-24-2009
uwork72 uwork72 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 32
awk check for equal - help

Input file:

Code:
x A 10
y A 10
z A 10
x B 10
y B 12
z B 10
x C 0
y C 0

Required output:

Code:
x B 10
y B 12
z B 10

i.e. printing only that section (based on 2nd field) for which third field($3) is not equal for all the lines for that 2nd field. Please help
  #2 (permalink)  
Old 02-24-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by uwork72 View Post
Input file:

Code:
x A 10
y A 10
z A 10
x B 10
y B 12
z B 10
x C 0
y C 0

Required output:

Code:
x B 10
y B 12
z B 10

i.e. printing only that section (based on 2nd field) for which third field($3) is not equal for all the lines for that 2nd field. Please help
Hmm..... this is a bit confusing...
why do you have records in red then?

Code:
x B 10
y B 12
z B 10

Could you rephrase the description, please.
  #3 (permalink)  
Old 02-24-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311
It's easy to produce the desired ouput but I didn't understand your explanaton.

Quote:
for which third field($3) is not equal for all the lines for that 2nd field
So you want those having $2 == B and also the whole line being unique?
If so...


Code:
grep " B " infile| uniq

  #4 (permalink)  
Old 02-24-2009
uwork72 uwork72 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 32
Thanks Zaxxon and vgersh99 for the reply.

I want to only print the section for which 3rd field is different (one section is basically based on second field, here A,B and C ).

Code:
x A 10
y A 10
z A 10
x B 10
y B 12
z B 10
x C 0
y C 0

  #5 (permalink)  
Old 02-24-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by uwork72 View Post
Thanks Zaxxon and vgersh99 for the reply.

I want to only print the section for which 3rd field is different (one section is basically based on second field, here A,B and C ).

Code:
x A 10
y A 10
z A 10
x B 10
y B 12
z B 10
x C 0
y C 0
ok, so what the output of the above?
  #6 (permalink)  
Old 02-24-2009
uwork72 uwork72 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 32
Quote:
Originally Posted by vgersh99 View Post
ok, so what the output of the above?

Code:
x B 10
y B 12
z B 10

for A, all the third fields are same (10), same for C(0), but for B the 3rd fields are not same, so I wanted to print B section.
  #7 (permalink)  
Old 02-26-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092
hi , seems perl is a little bit eaiser to handle


Code:
#!/usr/bin/perl
use strict;
my (%hash,%h);
open FH,"<a";
while(<FH>){
  my @tmp=split(" ",$_);
  $hash{$tmp[1]}.=$_;
  $h{$tmp[1]}->{$tmp[2]}=1;
}
close FH;
for my $key (sort keys %hash){
  my @tmp=keys %{$h{$key}};
  print $hash{$key} if $#tmp>=1;
}

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0