Sponsored Content
Top Forums Shell Programming and Scripting Need a quick and dirty solution Post 302664237 by alister on Friday 29th of June 2012 06:50:32 AM
Old 06-29-2012
The suggestions mentioned below are all incorrect for reasons stated in post #3.

Quote:
Originally Posted by elixir_sinari
Code:
awk -F"[ v]" 'a[$1]+0<$3+0{a[$1]=$3} END{for(i in a) print i" v"a[i]}' inputfile

Incorrect numeric comparison. Numerically, 1.10 is less than 1.9, but as a version number, 1.10 is greater than 1.9. To perform the comparison numerically and correctly, you'd have to transform the version number in some way. Perhaps something like a*1000 + b, where a is the first component of the version string and b is the second. Obviously, though, b can never be greater than or equal to the constant multiplier, or there would be some ambiguity.


Quote:
Originally Posted by expert
try below
Code:
for var in `awk -F " " '{print $1}' test_file_data | uniq`
do
  grep $var test_file_data | sort | tail -1
done

A lexicographical sort is inappropriate for a version string. Comparing as strings, 1.9 is greater than 1.10 since 9 follows 1 in the collation sequence of every locale of which I'm aware. Further, lexicographical sort results can vary with locale.

Why specify the field separator in awk -F " "? A single space is already the default value for FS. awk treats that specially. A single space ignores leading and trailing blank characters and splits on sequences of blanks (in the C/POSIX locale, spaces and tabs). If you intended to split on spaces only, you must use a regular expression bracket expression: awk -F '[ ]'.

grep $var is vulnerable to regular expression metacharacters in the first column of the data. This may or may not be a concern, depending on what the real data in that first column looks like. Regardless, fixed string matching, -F, is the correct approach. You do not want to treat the contents of that first column as a regular expression that can match strings that are not exactly identical to itself. You want to treat that column literally. Consider it a bonus that fixed string matching is also simpler and faster.


Quote:
Originally Posted by jayan_jay
Code:
$ sort -nr infile | nawk '!x[$1]++'
b v3.0
a v1.2
$

That is performing a lexicographical sort. See the previous example's critique for why a lexicographical sort is inappropriate.

Why? sort -nr will look for a numeric string at the very beginning of the line. If it doesn't find one, it will behave as if it read a zero. Since the first field in the sample data is alphabetic, all lines will numerically compare as equal to zero and to each other, which then requires sort to break that tie by performing a lexicographical sort on the entire line. Long story short, -n may as well not have been specified; given the sample data (and any data set where the first non-blank sequence is not a valid numeric string), sort -nr and sort -r give identical results.

Regards,
Alister

Last edited by alister; 06-29-2012 at 08:54 AM..
These 3 Users Gave Thanks to alister For This Post:
 

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:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy