The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Validating XSL sheet data in Unix Data file ravijunghare UNIX for Dummies Questions & Answers 1 11-07-2008 09:32 AM
extract lines based on few conditions prvnrk Shell Programming and Scripting 4 10-17-2008 05:24 AM
validating a file or directory new2Linux Shell Programming and Scripting 3 06-27-2008 12:44 AM
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-10-2007 02:25 AM
Validating inputs from a file sendhilmani123 Shell Programming and Scripting 1 05-10-2006 05:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-31-2008
trichyselva trichyselva is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 65
validating a file based on conditions

i have a file in unix in which the records are like this

aaa 123 233
aaa 234 222
aaa 242 222
bbb 122 111
bbb 122 123
ccc 124 222

In the output i want only the below records

aaa
ccc

The validation logic is 1st column and 2nd column need to be considered
if both columns values are not same and 1st column values are same
then the record in 1st column need to be picked up

in the records if the first and second column matches then those records need to be dropped

plz. let me know how to do this validation
  #2 (permalink)  
Old 12-31-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Use nawk or /usr/xpg4/bin/awk on Solaris:
Code:
awk 'END { 
  for (_ in u) if (u[_])
	  print _
	}
{
  u[$1] = k[$1,$2]++ ? x : 1      
  }' infile

Last edited by radoulov; 12-31-2008 at 09:44 AM.. Reason: refactored
  #3 (permalink)  
Old 12-31-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi radoulov,

another one of your astonishing awk scripts. Could you go in
some detail how it works.

Regards

Chris
  #4 (permalink)  
Old 12-31-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by Christoph Spohr View Post
Hi radoulov,

another one of your astonishing awk scripts. Could you go in
some detail how it works.

Regards

Chris
Astonishing ,
thank you!

I'll try to explain.
Code followed by comments.

Code:
{
  u[$1] = k[$1,$2]++ ? x : 1      
  }
While reading the input build an associative array named u (unique) keyed by $1. The values are build/chosen based on the following expression:

Code:
k[$1,$2]++ ? x : 1
If the value of the auto incremented associative array k, build en passant with $1 SUBSEP $2 as keys, is different than 0 (i.e. true in boolean context), i.e. already seen (remember Ed Morton's !arr[val]++?),
then return and assign the value of the variable x (never used and auto initialized -> null -> 0 in numeric context -> false in boolean context, if I had written 0, it would have been clearer ), otherwise return and assign the value 1 (the opposite of the previous).

Code:
END { 
  for (_ in u) if (u[_])
      print _
    }
After reading all the input, print only those u keys whose values are true when evaluated in boolean context (which equal to 1).

Happy holidays!

Last edited by radoulov; 12-31-2008 at 01:18 PM..
  #5 (permalink)  
Old 01-02-2009
trichyselva trichyselva is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 65
validating a input file in unix

hi,
thanks for the response
actually in my message if there are 3 records like

aaa 123 233
aaa 234 222
aaa 242 222

then only ONE aaa

need to be printed
but in the output it is showing all the 3 values

Actually in my input file it will contain nearly 10 fields each separated by pipe symbol
For that thing whether this solution will work (by replacing k[$1,$2]++ with all the fields like $3...) or i have to use another approach
I have to consider the first 2 fields for validation remaining fields i can leave as it is


expecting your reply

thanks
  #6 (permalink)  
Old 01-02-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
hi, you may try below perl script

Code:
#! /usr/bin/perl
open FH,"<a.txt";
while(<FH>){
	my @tmp=split(" ",$_);
	if(! exists $hash{$tmp[0]}){
		$hash{$tmp[0]}=$tmp[1]." " ;
		next;
	}
	if((exists $hash{$tmp[0]}) && ($hash{$tmp[0]} ne 'DUP')){
		$hash{$tmp[0]}=($hash{$tmp[0]} =~ m/$tmp[1] /)?'DUP':$hash{$tmp[0]}.$tmp[1]." ";		
	}
}
close FH;
print join "\n", grep {$hash{$_} ne 'DUP' } keys %hash;
  #7 (permalink)  
Old 01-02-2009
trichyselva trichyselva is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 65
hi,
I have to use shell script
please suggest some logic in shell script itself
i haven't used perl script

thanks
Sponsored Links
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 05:09 AM.


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