Hi,

I have a list of data in text files with :-
ColA ColB ColC ColD ColE ColF
12 122 2280 30 0.012987 1.776
13 118 2280 30 0.012987 1.954
14 115 2620 30 0.011321 2.131
15 113 2654 30 0.011177 2.309
16 145 1882 25 0.013110 2.487
17 112 2817 30 0.010537 2.664
18 126 2833 30 0.010479 2.842
19 111 2833 30 0.010479 3.020
16 145 1882 25 0.013110 2.487
17 112 2817 30 0.010537 2.664

How could I grep the nearest number to <=3.000 using bash script?

Means in the end i would like to isolate numbers in ColF which last seen the value <= 3.000 which is

18 126 2833 30 0.010479 2.842


Please help. Thanks.

Dani AI

Generated

A few clarifications and a small, robust approach you can drop on top of the thread.

The usual interpretation here is: "find the largest numeric value in the last column that is <= 3.0, and print the full row for that value." That is different from "closest by absolute distance" or "the last time the file went below 3.0" — pick the rule you actually want. originally asked for the max <= threshold. showed a simple numeric approach and demonstrated a full-row solution; both are good starting points, but watch out for edge cases below.

A single-pass awk that keeps the best value and its full line (and prefers later duplicates) — safe for large files and simple to adapt:

awk '
BEGIN { threshold = 3.0; found = 0 }
{
  val = ($NF) + 0
  if (val <= threshold) {
    if (!found || val >= best) {
      best = val
      best_line = $0
      found = 1
    }
  }
}
END {
  if (found) print best_line
}
' inputfile

If you prefer a small script (easier to extend for CSV, different separators, logging), here is a Python variant that does the same and skips non‑numeric last fields:

#!/usr/bin/env python3
import sys
threshold = 3.0
best = None
best_line = None
fh = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
for raw in fh:
    s = raw.strip()
    if not s or s.startswith("#"):
        continue
    parts = s.split()
    try:
        val = float(parts[-1])
    except (IndexError, ValueError):
        continue
    if val <= threshold and (best is None or val >= best):
        best = val
        best_line = raw.rstrip("\n")
if best_line:
    print(best_line)

Quick troubleshooting notes:

  • If your file has a header, skip it (check for non-numeric last field).
  • Initialise carefully (don’t assume values are positive).
  • If decimals can use commas or locale-dependent formats, normalize first.
  • If you actually want "closest absolute value" instead of "max <= threshold," track |threshold - val| and minimize that instead.

Both and had useful solutions; the snippets above aim to be a bit more defensive (header/non-numeric handling, correct tie-breaking and negative values) while staying single-pass and efficient.

Recommended Answers

All 6 Replies

Hey there,

This was bugging me all last night, and I finally figured out what my problem was ;)

This should do the trick for you:

awk 'BEGIN {var=3; highest=0}{ j = $NF;if ( j < var && j > highest ) { highest=j} } END {print highest}' YourInputFile

That gave me

2.842

from the text you pasted in this post. There's probably a more elegant solution out there, but this'll do the trick for now :)

Best wishes :)

, Mike

Actually, oops, you can change the

j < var

part to

j <= var

just noticed that :)

awk 'BEGIN {var=3; highest=0}{ j = $NF;if ( j <= var && j > highest ) { highest=j} } END {print highest}' YourInputFile

, Mike

awk '$NF <=3 {a[NR]=$NF;c[a[NR]]=$0}
END{
 n=asort(a,b)
 print c[b[n]] 
}' file

output:

# ./test.sh
18 126 2833 30 0.010479 2.842

I knew there was a better answer out there :)

GhostDog74, you are an awk master! If you don't mind me posting this here, could you point me to a good awk tutorial on the web. I'd like to better understand a lot of programmatical aspects of it.

Thanks in advance and good stuff :)

Cheers,

Mike


GhostDog74, you are an awk master!

no i am not. there are many who are better:)

If you don't mind me posting this here, could you point me to a good awk tutorial on the web. I'd like to better understand a lot of programmatical aspects of it.

this is where i learn AWK/SED. Also grymoire is worth a look

Cool,

Thank you very much :) I'll definitely check out these links (already adding them to my favorites).

You're very humble and come up with some excellent answers. I'm sure I'll see more in the future and I look forward to it :)

Thanks, again!

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.