Computing dataset for a specific record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Computing dataset for a specific record
# 1  
Old 07-14-2009
Question Computing dataset for a specific record

Hello everybody,
I want to compute a data file in awk. I am new in awk and I need your help. The data file has the following fields. It has thousands of records.

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

Now I want to search this data for every specific record (e.g Fre in column3) and then select the minimum value of that specific record in column5.Consider Col4 as a primary key(unique value for every record).
The output should be like this:

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

Thank you so much for your cooperation.
Regards,
Ubee
# 2  
Old 07-14-2009
This ex. give tools to create dynamic tool for your needs.
Code:
# search only some lines
awk -v searchfld=3  -v searchvalue="Fre" -v minfld=5  -v keyfld=4 '
   BEGIN {
             minvalue=9999999999999999
         }
   ( $minfld < minvalue ) && ( $searchfld == searchvalue ) {
                        minkey=$keyfld
                        minvalue=$minfld
                      }
END {
        print "minkey:",minkey," minvalue:",minvalue
    } '  inputfile

And solution for your needs:
Code:
awk -v keyfld=3  -v minfld=5  '
   BEGIN {
             minvalue=9999999999999999
         }
   Min[$keyfld]<1 { Min[$keyfld]=minvalue  }
   $minfld < Min[$keyfld]  {
                      lines[$keyfld]=$0
                      Min[$keyfld]=$minfld
                      }
END {
        for (id in lines) {
                print lines[id]
                }
    } ' inputfile

# 3  
Old 07-14-2009
thanks alot kshji
it is working perfectly!!!
well done
# 4  
Old 07-14-2009
Try:

Code:
awk ' NR>1 {if(arr[$3]=="") arr[$3]=$5; if(arr[$3] >= $5) { arr[$3]=$5; sarr[$3]=$0; }} END { for (i in sarr) { print sarr[i];  } }'  filename

# 5  
Old 07-14-2009
yes dennis , it is also working but in this case we are considering Col3 as a key field.
I am thankful for your response.

---------- Post updated at 11:02 PM ---------- Previous update was at 09:43 PM ----------

ok after finding the specific record with minimum Col5 value, just like the output as

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 how to find the specific record with max Col5 value , just like the output as

0.47 0.08 nie 25:76 32

I appreciate your help.
Regards,
Ubee
# 6  
Old 07-15-2009
Code:
# max value of some fld
awk -v fld=5  '
   BEGIN {
             value=-9999999999999999
         }
   $fld > value {
                    line=$0
                    value=$fld
                    }
END {
        print line
    } '  inputfile


Last edited by kshji; 07-15-2009 at 06:27 AM..
# 7  
Old 07-15-2009
Code:
awk 'NR > 1 && $NF > a{a=$NF;line=$0}END{print line}' file

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