Sponsored Content
Top Forums Shell Programming and Scripting Need a quick and dirty solution Post 302664319 by drl on Friday 29th of June 2012 10:15:57 AM
Old 06-29-2012
Hi, elixir_sinar.
Quote:
Originally Posted by elixir_sinari
Is there any problem in this solution, alister?

Code:
awk -F"[ v.]" '$3+0==vleft[$1]+0 && $4"">vright[$1]"" {vright[$1]=$4} $3+0>vleft[$1]+0 {vleft[$1]=$3;vright[$1]=$4} END {for(i in vleft) print i" v"vleft[i]"."vright[i]}' inputfile

Well, let us try it:
Code:
#!/usr/bin/env bash

# @(#) user2	Demonstrate proposed awk solution for "version" strings.

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 awk

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

pl " Results:"
# awk -F"[ v]" 'a[$1]+0<$3+0{a[$1]=$3} END{for(i in a) print i" v"a[i]}' $FILE
awk -F"[ v.]" '$3+0==vleft[$1]+0 && $4"">vright[$1]"" {vright[$1]=$4} $3+0>vleft[$1]+0 {vleft[$1]=$3;vright[$1]=$4} END {for(i in vleft) print i" v"vleft[i]"."vright[i]}' $FILE

exit 0

producing:
Code:
% ./user2

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
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:
a v1.2
b v3.0
c v3.9
d v4.0

That does not look correct to me. Did I paste your code faithfully? Are your results different?

Best wishes ... cheers, drl

---------- Post updated at 09:15 ---------- Previous update was at 09:08 ----------

Hi.

With alister's correction:
Code:
#!/usr/bin/env bash

# @(#) user2	Demonstrate proposed awk solution for "version" strings.

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 awk

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

pl " Results:"
# awk -F"[ v]" 'a[$1]+0<$3+0{a[$1]=$3} END{for(i in a) print i" v"a[i]}' $FILE
# awk -F"[ v.]" '$3+0==vleft[$1]+0 && $4"">vright[$1]"" {vright[$1]=$4} $3+0>vleft[$1]+0 {vleft[$1]=$3;vright[$1]=$4} END {for(i in vleft) print i" v"vleft[i]"."vright[i]}' $FILE
awk -F"[ v.]" '$3+0==vleft[$1]+0 && $4+0>vright[$1]+0 {vright[$1]=$4} $3+0>vleft[$1]+0 {vleft[$1]=$3;vright[$1]=$4} END {for(i in vleft) print i" v"vleft[i]"."vright[i]}' $FILE

exit 0

producing:
Code:
% ./user2

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
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:
a v1.2
b v3.0
c v3.10
d v4.0

Seems OK for 2-part version strings ... cheers, drl
 

3 More Discussions You Might Find Interesting

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

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. 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
All times are GMT -4. The time now is 08:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy