awk check for equal - help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk check for equal - help
# 8  
Old 02-24-2009
ok,u got ur ans.
# 9  
Old 02-24-2009
Quote:
Originally Posted by salil2012
ok,u got ur ans.
Salil, do you have a solution to this ? I am not sure why you are putting unnecessary comments on this post. A solution/help from you is much appreciated :-)

Last edited by uwork72; 02-24-2009 at 11:44 AM.. Reason: Typo
# 10  
Old 02-24-2009
not a very decent solution this. but give it a try.
Code:
$ cat file
x a 10
y a 10
z a 11
x b 10
y b 12
z b 10
x c 0
y c 0
x d 1
y d 1
z d 1
w d 2
x e 1
y e 1
z e 1

a,b and d are the records you want printed from "file".

printing out the required "$2"s below....
Code:
$ sort -u -k 2,3 file | nawk 'BEGIN {a=$2} {if($2==a && NF > 0){print $2}}{a=$2}'
a
b
d

and this is your solution....
Code:
$ for i in `sort -u -k 2,3 file | nawk 'BEGIN {a=$2} {if($2==a && NF > 0){print $2}}{a=$2}'`; do nawk -v var=$i '$2 == var {print $0}' file; done
x a 10
y a 10
z a 11
x b 10
y b 12
z b 10
x d 1
y d 1
z d 1
w d 2

cheers.
rrk001

Last edited by Franklin52; 02-24-2009 at 03:11 PM.. Reason: adding code tags
# 11  
Old 02-24-2009
if you dont want to call nawk for each record that meets the criteria you can print the required "$2"s to a file and fetch them into an array using getline and split.
# 12  
Old 02-26-2009
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;
}

# 13  
Old 02-27-2009
yes. it really does. compact piece of code there. but, if you still want to use shell scripts Smilie here you go.
Code:
#! /bin/bash
gawk 'BEGIN{
while (getline < "file") {
  twothree[$2$3]=$2;
}
for (i in twothree) {
  if (twothree[i] in two) {
    filtered[twothree[i]];}
  else {
    two[twothree[i]];}
}}
{if ($2 in filtered) {print $0;}
}' file

cheers!!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

awk not equal

Did I do something wrong with this awk not equal? For some reason it prints twice. >awk '{if ($4 != "root") print $1 " " $4 " " $5}' ls_test server10: njs nodeadm server10: njs nodeadm >grep server10 ls_test server10: drwxr-sr-x. 18 njs nodeadm 4096 Aug 16 09:42 /opt > (2 Replies)
Discussion started by: cokedude
2 Replies

2. Shell Programming and Scripting

awk to print record not equal specific pattern

how to use "awk" to print any record has pattern not equal ? for example my file has 5 records & I need to get all lines which $1=10 or 20 , $2=10 or 20 and $3 greater than "130302" as it shown : 10 20 1303252348212B030 20 10 1303242348212B030 40 34 1303252348212B030 10 20 ... (14 Replies)
Discussion started by: arm
14 Replies

3. Shell Programming and Scripting

AWK splitting a string of equal parts of 500 chars

Hi , can someone help me how to make an AWK code for splitting a string of equal parts of 500 chars in while loop? Thank you! (4 Replies)
Discussion started by: sanantonio7777
4 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

Using awk, print all the lines where field 8 is equal to x

Using awk, print all the lines where field 8 is equal to x I really did try, but this awk thing is really hard to figure out. file1.txt"Georgia","Atlanta","2011-11-02","x","","","","" "California","Los Angeles","2011-11-03","x","","","",""... (2 Replies)
Discussion started by: charles33
2 Replies

6. Shell Programming and Scripting

awk - setting fs to equal any single character

Hi Does anyone know how to set any character as the field separator with awk/nawk on a solaris 10 box. I have tried using /./ regex but this doesnt work either and im out of ideas. thanks (7 Replies)
Discussion started by: chronics
7 Replies

7. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

8. Shell Programming and Scripting

awk field equal something, then add something to the field

Hi Everyone, a.txt a b c 1 e e e e e a b c 2 e e e e e the output is a b c 1 e e e e e a 00b c 2 e e e e e when 4th field = '2', then add '00' in the front of 2nd field value. Thanks (9 Replies)
Discussion started by: jimmy_y
9 Replies

9. Shell Programming and Scripting

while [ $x -ge 50 ] + and equal to zero ; then

while + and equal to zero ; then what to punt instead of phrase and equal to zero. it's bash thank you in advance (1 Reply)
Discussion started by: losh
1 Replies
Login or Register to Ask a Question