bash get larger numerical match from 2 lines
greetings. i have output which looks like the following:
Jan 10 joe 2017 10:15:10 something for 2 but not from 3 pcs Jan 10 joe 2017 10:15:10 something for 2 but not from 4 pcs Jan 10 joe 2017 10:15:10 something for 2 but not from 1 pcs I would like to just print the line which has the higher value based on the 12th column which is the '4' in this example Suggestions please ? Thanks again, |
awk
|
Hey bishop2001,
You could try: Code:
cat data.txt | sort -nrk12 | head -n 1 HTH, Dave |
With this InFile ...
Code:
Jan 10 joe 2017 10:15:10 something for 2 but not from 3 pcs Code:
sort -rk12,1 <$InFile \ Code:
Jan 10 joe 2017 10:15:10 something for 2 but not from 4 pcs Code:
awk '{if ($12>best) best=$0} \ Code:
Jan 10 joe 2017 10:15:10 something for 2 but not from 3 pcs Daniel B. Martin |
Quote:
Code:
awk '{if ($12>best) { best=$12; bestline=$0 } } \ |
Quote:
Daniel B. Martin |
As pointed out, having the variable for the test and the storage is not the correct way to go.
Alternative awk (just for fun): Code:
awk '{a[$12]=$0}END{print a[b[asorti(a,b)]]}' file |
Quote:
{LQ won't let me rep you again, darnit.} Daniel B. Martin |
Quote:
Daniel B. Martin |
Quote:
Here is one with bash only: Code:
#!/bin/bash |
More bash :)
Code:
#!/usr/bin/env bash |
Very nice grail!
And agreed, it would be nice to see the OP participate! |
Quote:
By the way, in post #4 the variable best and in post #10 maxnum and in post #11 keep were not initialised, which in this case is ok, but in general it may cause misbehaviour. The usual way is to use the first data line to start with. |
Quote:
I do follow what you mean about initialising, but normally only worry in languages where it is a gotcha, like ruby :( |
Several solutions have been posted, and they fall into two categories: some use a sort, others use a single pass through the data. If the input file is large (thousands of lines) intuition suggests that "single pass" should be preferred because it will run in less time.
Right? Wrong? Opinions? Daniel B. Martin |
All times are GMT -5. The time now is 10:29 PM. |