Need a quick and dirty solution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a quick and dirty solution
# 1  
Old 06-28-2012
Need a quick and dirty solution

I have a list of multiple versions of software. The list is formated as follows:

NAME VERSION

I simply need to pull out the highest version of each software, for example:

Original File
Code:
a v1.0
a v1.1
a v1.2
b v2.1
b v2.2
b v2.21
b v3.0

Output
Code:
a v1.2
b v3.0

# 2  
Old 06-28-2012
Code:
$ awk '(!A[$1]) || (A[$1] < $2) { A[$1]=$2 }; END { for (X in A) { print X, A[X] } }' <<EOF
a v1.0
a v1.1
a v1.2
b v2.1
b v2.2
b v2.21
b v3.0
EOF

a v1.2
b v3.0

$

# 3  
Old 06-28-2012
Quote:
Originally Posted by Corona688
Code:
$ awk '(!A[$1]) || (A[$1] < $2) { A[$1]=$2 }; END { for (X in A) { print X, A[X] } }' <<EOF

Given that the sample version data contains components of varying widths, e.g. 2.2 and 2.21, I don't think this code is sufficient.

The highlighted expression is a string comparison. v3.9 will be considered greater than v3.10. If the comparison were made using numeric substrings, 3.9 would still be greater than 3.10.

Regards,
Alister

---------- Post updated at 07:04 PM ---------- Previous update was at 07:00 PM ----------

Quick and dirty, as per the topic:
Code:
tr . '\t' < file | sort -k1,1 -k2.2b,2nr -k3,3nr | awk 'p!=$1 {p=$1; print}' | tr '\t' .

Assumes there is only the one dot in the version numbers, that there are no tabs in the file, and that your field delimiter is a space. If the delimiter is a tab and there are no spaces, change \t to a space.

Regards,
Alister

Last edited by alister; 06-28-2012 at 08:15 PM..
# 4  
Old 06-28-2012
Hi.

Using available (but non-standard) utility msort:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate msort hybrid sort.
# See: http://freecode.com/projects/msort

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort awk

FILE=${1-data1}
pl " Input data $FILE:"
cat $FILE

pl " Results:"
msort -q -l -n 2,2 -I -c hybrid $FILE |
tee f1 |
awk '!a[$1]++'

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
msort 8.44
awk GNU Awk 3.1.5

-----
 Input data data1:
d v4.0.9
d v4.0.10
a v1.0
a v1.1
a v1.2
b v2.1
b v2.2
b v2.21
b v3.0
c v3.10
c v3.9

-----
 Results:
d v4.0.10
c v3.10
b v3.0
a v1.2

The intermediate results are on file f1. The awk prints the first occurrence of an item, skipping the rest.

If msort is not in your repository, see link in script for binary or source to compile.

Best wishes ... cheers, drl
# 5  
Old 06-29-2012
Code:
awk -F"[ v]" 'a[$1]+0<$3+0{a[$1]=$3} END{for(i in a) print i" v"a[i]}' inputfile

# 6  
Old 06-29-2012
try below
Code:
for var in `awk -F " " '{print $1}' test_file_data | uniq`
do
  grep $var test_file_data | sort | tail -1
done

hope this would work out as simple..

Last edited by Franklin52; 06-29-2012 at 04:10 AM.. Reason: Please use code tags for data and code samples
# 7  
Old 06-29-2012
Code:
$ sort -nr infile | nawk '!x[$1]++'
b v3.0
a v1.2
$

Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. What is on Your Mind?

Anybody want to talk about Dirty Cow?

Hi All, How worried is everyone about the Dirty Cow Linux exploit? Has anybody experienced attacks yet? From the research I've done it seems that the exploit is "reliable" (that is it works nearly every time on vulverable systems) which is not good news. We all believe that Unix/Linux... (3 Replies)
Discussion started by: hicksd8
3 Replies

2. UNIX for Advanced & Expert Users

Superblock marked dirty

Good morning! I met a problem on a FS with AIX 5.3 It's not possible to mount the FS because of a dirty superblock. I tried few things without success. I need your help to solve my problem guys. Do you have any idea please? Thanks a lot drp01,/home/root # mount /GSPRES/data Replaying... (9 Replies)
Discussion started by: Castelior
9 Replies

3. Shell Programming and Scripting

Quick-and-dirty g++ compilation

I am creating a small bash file that will take one argument and compile it: #!/bin/bash OUT=${$1%.cpp} # C++ source files always end in .cpp g++ -Wall $1 -o $OUT chmod 777 $OUT The error message says 'bad substitution', namely where OUT is defined. How to fix this? (1 Reply)
Discussion started by: figaro
1 Replies
Login or Register to Ask a Question