Displaying lines of a file which have the highest number?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Displaying lines of a file which have the highest number?
# 1  
Old 04-19-2010
Displaying lines of a file which have the highest number?

Hello

Wondering if anybody may be able to advise on how I can filter the contents of the following file:

Code:
 
<object_name>-<version>          <Instance>        

GM_GUI_code.fmb-4                        1
GM_GUI_code.fmb-5                        1
GM_GUI_code.fmx-4                        1
GM_GUI_code.fmx-5                        1
GM_GUI_code.fmx-4                        2
GM_GUI_code.fmx-5                        2
GM_Extract_dbcode.pkb-18              1
GM_Extract_dbcode.pks-18              1 
GM_Extract_dbcode.pkb-19              1 
GM_Extract_dbcode.pks-19              1 
GM_Extract_dbcode.pkb-20              1 
GM_Extract_dbcode.pks-20              1 

This is a list of the contents of a release. I need display only the LATEST version (version number is after the file extension) of an object so I would then only have the following displayed:

Code:
 
<object_name>-<version>          <Instance>

GM_Extract_dbcode.pkb-20              1 
GM_Extract_dbcode.pks-20              1
GM_GUI_code.fmb-5                        1
GM_GUI_code.fmx-5                        1
GM_GUI_code.fmx-5                        2


As you can see there are two instances of GM_GUI_code.fmx. I need to be able to display both instances. I'm a bit stumped as to how to do this though.

Thanks
Glyn
# 2  
Old 04-19-2010
Code:
# awk -F'[ -]' 'NR==FNR{if(NR<3)print;x=($2>x && /GUI/)?$2:x;y=($2>y && /Extract/)?$2:y;next}$2==x || $2==y{a[$0]}END{for (i in a) print i}' file file
<object_name>-<version>          <Instance>

GM_Extract_dbcode.pkb-20              1
GM_Extract_dbcode.pks-20              1
GM_GUI_code.fmb-5                        1
GM_GUI_code.fmx-5                        1
GM_GUI_code.fmx-5                        2

# 3  
Old 04-19-2010
Thanks Danmero. Unfortunately I need to be able to run the command on different files, in which the object names listed won't don't contain "GUI" in the name...
# 4  
Old 04-19-2010
Quote:
Originally Posted by Glyn_Mo
Thanks Danmero. Unfortunately I need to be able to run the command on different files, in which the object names listed won't don't contain "GUI" in the name...
Feel free to adjust the code as you wish, unfortunately I can't provide you a universal one-size-fit-all solution.
If you need something else please post an extended data sample.
# 5  
Old 04-19-2010
Try:
Code:
awk -F'[- \t]*' '{A[$1,$3]=$2} END{for (i in A){split (i, B, SUBSEP); printf "%-25s %s\n",B[1]"-"A[i],B[2]}}' infile

Code:
GM_GUI_code.fmx-5         1
GM_GUI_code.fmb-5         1
GM_Extract_dbcode.pkb-20  1
GM_Extract_dbcode.pks-20  1
GM_GUI_code.fmx-5         2

Code:
$ cat infile
GM_GUI_code.fmb-4                        1
GM_GUI_code.fmb-5                        1
GM_GUI_code.fmx-4                        1
GM_GUI_code.fmx-5                        1
GM_GUI_code.fmx-4                        2
GM_GUI_code.fmx-5                        2
GM_Extract_dbcode.pkb-18              1
GM_Extract_dbcode.pks-18              1
GM_Extract_dbcode.pkb-19              1
GM_Extract_dbcode.pks-19              1
GM_Extract_dbcode.pkb-20              1
GM_Extract_dbcode.pks-20              1

# 6  
Old 04-19-2010
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk -F'[- \t]*' '{A[$1,$3]=$2} END{for (i in A){split (i, B, SUBSEP); printf "%-25s %s\n",B[1]"-"A[i],B[2]}}' infile

Code:
GM_GUI_code.fmx-5         1
GM_GUI_code.fmb-5         1
GM_Extract_dbcode.pkb-20  1
GM_Extract_dbcode.pks-20  1
GM_GUI_code.fmx-5         2

This work for me:
Code:
# awk -F'[- \t]*' '{if(NR<3){print;next}}{A[$1,$3]=$2} END{for (i in A){split (i, B, SUBSEP); printf "%-25s %s\n",B[1]"-"A[i],B[2]}}' file
<object_name>-<version>          <Instance>

GM_Extract_dbcode.pks-20  1
GM_Extract_dbcode.pkb-20  1
GM_GUI_code.fmx-5         1
GM_GUI_code.fmx-5         2
GM_GUI_code.fmb-5         1

# 7  
Old 04-22-2010
Scrutinizer, many thanks.

I've had a problem when running this against another file though. The file contents are as follows:

Code:
bash-3.00$ cat release
ins_smo.sql-22                           1
lfa10ins.fmb-1                           1
lfa10ins.fmb-2                           1
lfa10ins.fmx-1                           1
lfa10ins.fmx-1                           2
lfa10ins.fmx-2                           1
lfa10ins.fmx-2                           2
pen_pack_05.pkb-8                        2
pkg_land_insp_capture_form.pkb-19        1
release_note_SAF10_Certification_1.doc-0 1
release_note_SAF10_Certification_1.doc-1 1
release_note_SAF10_Certification_1.doc-2 1
releasenote-1317                         1
saf09127.rdf-10                          1
saf09127.rdf-9                           1
saf09127.rep-10                          1
saf09127.rep-10                          2
saf09127.rep-9                           1
saf09127.rep-9                           2
saf1016c.fmb-1                           1
saf1016c.fmb-2                           1
saf1016c.fmx-1                           1
saf1016c.fmx-1                           2
saf1016c.fmx-2                           1
saf1016c.fmx-2                           2
saf1016m.fmb-1                           1
saf1016m.fmx-1                           1
saf1016m.fmx-1                           2
saf1016s.fmb-1                           1
saf1016s.fmx-1                           1
saf1016s.fmx-1                           2
bash-3.00$

When I run your command, I get the following returned:
Code:
saf09127.rep-9            2
releasenote-1317          1
ins_smo.sql-22            1
saf1016s.fmx-1            1
saf1016c.fmb-2            1
saf1016s.fmx-1            2
saf1016s.fmb-1            1
pen_pack_05.pkb-8         2
saf1016m.fmx-1            1
saf1016m.fmx-1            2
pkg_land_insp_capture_form.pkb-19 1
saf1016m.fmb-1            1
lfa10ins.fmx-2            1
lfa10ins.fmx-2            2
lfa10ins.fmb-2            1
release_note_SAF10_Certification_1.doc-2 1
saf1016c.fmx-2            1
saf1016c.fmx-2            2
saf09127.rdf-9            1
saf09127.rep-9            1

The problem is that objects
Code:
saf09127.rdf-9            1
saf09127.rep-9            1
saf09127.rep-9            2

... are being displayed, instead of
Code:
saf09127.rdf-10            1
saf09127.rep-10            1
saf09127.rep-10            2

Is there some problem when it's calculating double-figures?

I must the awk I'm seeing in these solutions is a bit beyond my current knowledge! Thanks to both. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding user name to file, and then displaying new line number

Hi all - I'm completely stumped by a script I'm working on... The short version is I have a file called 'lookup' and in it are hundreds of names (first and last). I have a script that basically allows the user to enter a name, and what I need to have happen is something like this: Record... (8 Replies)
Discussion started by: sabster
8 Replies

2. Programming

Help with find highest and smallest number in a file with c

Input file: #data_1 AGDG #data_2 ADG #data_3 ASDDG DG #data_4 A Desired result: Highest 7 Slowest 1 code that I try but failed to archive my goal :( #include <stdio.h> (2 Replies)
Discussion started by: cpp_beginner
2 Replies

3. Shell Programming and Scripting

Bash, finding highest number in another file

i have a problem i am working on and am completely new to bash commands. I writing a script to read another file and output the max and Min number in the script. I must use variables to output the max and min numbers. grades = file with numbers in them. This is what i got so far. Thank You in... (3 Replies)
Discussion started by: ctanner10126
3 Replies

4. Shell Programming and Scripting

How to only display lines where a field has the highest number?

Hello Wondering if anybody can advise me how I can sort the below file so it only displays lines with the latest versions of an object? As you'll see some of the scripts listed in my file have more than one version number (version number is after the file extension). E.g. cdm_bri.pkb has... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

5. Homework & Coursework Questions

Displaying specific lines from a CSV file

1. The problem statement, all variables and given/known data: Display from a csv file, birthdays that occur today. If there are no birthdays today, the next one in the year. 2. Relevant commands, code, scripts, algorithms: The csv file is ordered from older to younger (ie. the most recent... (8 Replies)
Discussion started by: Adzi
8 Replies

6. Shell Programming and Scripting

Finding line with highest number in a file

Hi All, My file looks some thing like this, File 1: - A 10 B 30 C 5 D 25 E 72 F 23 now my requirement is to find the line with highest number in it, i;e the result should be E 72 Thanks in Advance (1 Reply)
Discussion started by: balu_puttaganti
1 Replies

7. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

8. Shell Programming and Scripting

Displaying number of lines from file

Hi, I am using below command to display the number of line, but its returning no of lines along with file name. But i want only no of line in the variable p. Please help me on this? p=`wc -l "text file"` echo "$p" (6 Replies)
Discussion started by: shivanete
6 Replies

9. UNIX for Dummies Questions & Answers

Displaying specific lines in a file.

I'm trying to figure out how to display a certain line in a text file. I keep getting references to Tail and Head, and I know how these work, but i'm lost on how to find say the third out of the five lines and display only that. I thought maybe grep could help, but that doesn't seem likely. ... (3 Replies)
Discussion started by: MaestroRage
3 Replies

10. Shell Programming and Scripting

find the highest number in the file

Hi, I have a file a.txt and it has values in it Eg :- I need to read through the file and find the number that is the greatest in them all. Can any one assit me on this. Thanks (30 Replies)
Discussion started by: systemali
30 Replies
Login or Register to Ask a Question