Computing dataset for a specific record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Computing dataset for a specific record
# 8  
Old 07-15-2009
Code:
while(<DATA>){
	chomp;
	if($.==1){
		print $_,"\n";
		next;
	}
	my @tmp =split;
	my $key=$tmp[2]." ".$tmp[3];
	if(not exists $hash{$key}){
		$hash{$key}->{val}=$_;
		$hash{$key}->{min}=$tmp[4];
	}
	else{
		if($tmp[4] < $hash{$key}->{min}){
			$hash{$key}->{val}=$_;
		}
	}
}
foreach my $key(keys %hash){
	print $hash{$key}->{val},"\n";
}
__DATA__
Col1 Col2 Col3 Col4 Col5
0.85 0.07 Fre 42:86 25 
0.73 0.03 frp 21:10 28 
0.64 0.04 Fre 42:86 63 
0.47 0.08 nie 25:76 32
0.37 0.01 veb 00:71 26
0.63 0.48 Fre 42:86 55
0.65 0.32 frp 21:10 19
0.53 0.56 nie 25:76 52
0.32 0.43 veb 00:71 18

# 9  
Old 07-16-2009
Thank you so much for your help

---------- Post updated 07-16-09 at 05:13 PM ---------- Previous update was 07-15-09 at 11:02 PM ----------

ok as I mentioned in my first post that if we have the following dataset:

Col1 Col2 Col3 Col4 Col5
0.85 0.07 Fre 42:86 25
0.73 0.03 frp 21:10 28
0.64 0.04 Fre 42:86 63
0.47 0.08 nie 25:76 32
0.37 0.01 veb 00:71 26
0.63 0.48 Fre 42:86 55
0.65 0.32 frp 21:10 19
0.53 0.56 nie 25:76 52
0.32 0.43 veb 00:71 18

after computing it and selecting the records with minimum Col5 value.
Then we will get the following dataset

Col1 Col2 Col3 Col4 Col5
0.85 0.07 Fre 42:86 25
0.65 0.32 frp 21:10 19
0.47 0.08 nie 25:76 32
0.32 0.43 veb 00:71 18

Now after getting this dataset, I want to select the record with max Col5 value.
So the final record should be

0.47 0.08 nie 25:76 32

Is it possible to do it in one program?
As in the previous post, it is a separate program, i mean how can we merge the two programs together.
Thanks a lot,
Regards,
Ubee
# 10  
Old 07-16-2009
You can put to the same, but
Why to put in same ? You have two working small nice tools.
run first and output > newfile
run second using newfile as input ?
Or write bigger and harder ruleset to understand ? Why ?

Better solution is to make "mother script", which handle input and output using those little solutions. Main idea of *nix. (for me).
# 11  
Old 07-16-2009
Hi,

Can any one please tell me that what's wrong in the following code

Code:
BEGIN {FS="\t"; keyfield=4; minfield=5; minvalue=999999999999 }
$0!="" {
	min[$keyfield]<1 { min[$keyfield]=minvalue  }
	$minfield<min[$keyfield]  { lines[$keyfield]=$0; min[$keyfield]=$minfield  }
          }
END {	
             for (id in lines) {
                		     print lines[id]
                	          }
        }

When I run this program, I get two syntax errors,
Code:
	min[$keyfield]<1 { min[$keyfield]=minvalue  }
  	                          ^ syntax error
                         $minfield<min[$keyfield]  { lines[$keyfield]=$0; min[$keyfield]=$minfield  }
                                	                       ^ syntax error

Syntax Errors at two opening brackets.Please I need your help.
Thanks,
Regards,
Ubee

Last edited by Ygor; 07-16-2009 at 11:55 PM.. Reason: Added CODE tags
# 12  
Old 07-16-2009
You can't nest pattern/action pairs. Try...
Code:
BEGIN {FS="\t"; keyfield=4; minfield=5; minvalue=999999999999 }
$0=="" {next}
min[$keyfield]<1 { min[$keyfield]=minvalue  }
$minfield<min[$keyfield]  { lines[$keyfield]=$0; min[$keyfield]=$minfield  }
          
END {	
             for (id in lines) {
                		     print lines[id]
                	          }
        }

# 13  
Old 07-17-2009
Quote:
Originally Posted by ubeejani
Is it possible to do it in one program?
As in the previous post, it is a separate program, i mean how can we merge the two programs together.
Code:
awk ' NR>1 {if(a[$3]=="") a[$3]=$5;if(a[$3]>=$5) {a[$3]=$5; sa[$3]=$0;}}NR>1&&$NF>x{x=$NF;line=$0}END{for(i in sa){print sa[i]};printf "\n%s",line}' file
0.85 0.07 Fre 42:86 25
0.47 0.08 nie 25:76 32
0.65 0.32 frp 21:10 19
0.32 0.43 veb 00:71 18

0.47 0.08 nie 25:76 32

Please read the Forum Rules.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove footer record in specific condition

Hi Experts, we have a requirement , need your help to remove the footer record in the file. Input file : 1011070375,,21,,NG,NG,asdfsfadf,1011,,30/09/2017,ACI,USD,,0.28,,,,,,,,,,,, 1011070381,,21,,NG,NG,sgfseasdf,1011,,30/09/2017,ACI,GBP,,0.22,,,,,,,,,,,,... (6 Replies)
Discussion started by: KK230689
6 Replies

2. Shell Programming and Scripting

Help with print out record if first and next line follow specific pattern

Input file: pattern1 100 250 US pattern2 50 3050 UK pattern3 100 250 US pattern1 70 1050 UK pattern1 170 450 Mal pattern2 40 750 UK . . Desired Output file: pattern1 100 250 US pattern2 50 3050 UK pattern1 170 450 Mal pattern2... (3 Replies)
Discussion started by: cpp_beginner
3 Replies

3. Shell Programming and Scripting

Execution problem with print out record that follow specific pattern

Hi, Do anybody know how to print out only those record that column 1 is "a" , then followed by "b"? Input file : a comp92 2404242 2405172 b comp92 2405303 2406323 b comp92 2408786 2410278 a comp92 2410271 2410337 a comp87 1239833 1240418 b comp87... (3 Replies)
Discussion started by: patrick87
3 Replies

4. Shell Programming and Scripting

Help with print out line that have different record in specific column

Input file 1: - 7367 8198 - 8225 9383 + 9570 10353 Input file 2: - 2917 3667 - 3851 4250 + 4517 6302 + 6302 6740 + 6768 7524 + 7648 8170 + 8272 8896 + 8908 9915 - 10010 ... (18 Replies)
Discussion started by: perl_beginner
18 Replies

5. 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

6. Shell Programming and Scripting

Get last field specific record

i have file A as below contents --------------------------- Use descriptive thread titles when posting. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". For example, do not post questions For example, do not deliminated. output file as below:... (2 Replies)
Discussion started by: ANSHUMAN1983
2 Replies

7. Shell Programming and Scripting

sed substitution for specific record

Hi, I have a file with two different people, each with there own figure next to it. How would I use a sed command to try and change the correct corresponding value? or is another command more appropriate? I have tried sed -n '/dan/p' money | sed -i 's/1000/1500/g' money the file contents are... (5 Replies)
Discussion started by: somersetdan
5 Replies

8. Solaris

flarecreate for zfs root dataset and ignore multiple dataset

Hi All, I want to write a script to create flar images on multiple servers. In non zfs filesystem I am using -X option to refer a file to exclude mounts on different servers. but on ZFS -X option is not working. I want multiple mounts to be ignore on ZFS base system during flarecreate. I... (0 Replies)
Discussion started by: uxravi
0 Replies

9. Shell Programming and Scripting

Regarding multiline record searching with specific pattern

Dear Experts, I need to extract specific records from one file which has multiline records. Input file pattern is: ============ aaaaaaaa bbbbbbbb asdf 1234 cccccccc dddddddd ============ aaaaaaaa bbbbbbbb qwer 2345 cccccccc dddddddd (7 Replies)
Discussion started by: dhiraj4mann
7 Replies

10. Virtualization and Cloud Computing

Event Cloud Computing - IBM Turning Data Centers Into ?Computing Cloud?

Tim Bass Thu, 15 Nov 2007 23:55:07 +0000 *I predict we may experience less*debates*on the use of the term “event cloud”*related to*CEP in the future, now that both IBM and Google* have made announcements about “cloud computing” and “computing cloud”, IBM Turning Data Centers Into ‘Computing... (0 Replies)
Discussion started by: Linux Bot
0 Replies
Login or Register to Ask a Question