How to parse a decimal number using ‘awk’ and ‘cut’ in Linux

Updated

Linux terminal showing a file listing

awk '{print $2}' pulls a decimal field out no matter how the columns are spaced, and awk '$2 > 50' data.txt filters on it numerically — fields read straight from input are numeric strings. cut -d ' ' -f2 only works when the delimiter is truly fixed, and locale decides what counts as a decimal point — so force LC_NUMERIC=C in any script that parses decimals. All of it demonstrated below.

Here is the data file and the output of awk '$2 > 50 {print $0}' data.txt on it:

$ cat data.txt
Item1 12.34
Item2 45.67
Item3 89.01
$ awk '$2 > 50 {print $0}' data.txt
Item3 89.01

One line. awk got it right — fields read straight from input are what awk calls “numeric strings,” and comparing one against a numeric constant like 50 is a numeric comparison, not a text comparison.

The actual trap: numeric compares only sometimes

Once a value stops being a fresh field from input — once it’s a quoted literal, or has passed through a string operation — awk’s comparison rule switches to lexical (character-by-character), and single-digit-looking numbers lose to anything starting with a bigger digit:

$ awk 'BEGIN{ a=9; b=89.01; print (a>b) }'
0
$ awk 'BEGIN{ a="9"; b="89.01"; print (a>b) }'
1

Same values, opposite answers. As bare numeric constants, 9 > 89.01 is false, correctly. As quoted strings, awk compares them character by character: '9' vs. '8', and '9' sorts higher — so "9" > "89.01" comes back true. If you ever move a field into a string context (concatenation, sprintf, array-key lookups against string literals) before comparing it, verify the comparison is still doing what you think.

Locale changes what counts as a decimal point

This machine’s awk — the “one true awk” shipped on macOS — respects LC_NUMERIC for parsing decimal numbers, and the difference is real, not theoretical:

$ echo "12.34 99" | LC_NUMERIC=de_DE.UTF-8 awk '{print $1+0}'
12
$ echo "12.34 99" | awk '{print $1+0}'
12.34

Under de_DE.UTF-8, the decimal separator is a comma, so awk stops reading "12.34" at the period and treats the rest as garbage — $1+0 comes out 12, silently dropping the fraction. If a script that parses decimal log data ever runs under a non-English locale (a cron job with an inherited LC_NUMERIC, a container built from a non-US base image), this is the failure mode: no error, just wrong numbers. Force the locale explicitly at the top of scripts that parse decimals: LC_NUMERIC=C awk ....

Where cut breaks and awk doesn’t

cut -d splits on a literal delimiter, once per occurrence — it has no concept of “one or more spaces” as a single separator. Give it a file with inconsistent spacing and it returns empty fields where awk‘s default whitespace splitting still works:

$ cat -e data_ws.txt
Item1   12.34$
Item2 45.67$
Item3    89.01$
$ cut -d ' ' -f2 data_ws.txt
(blank)
45.67
(blank)
$ awk '{print $2}' data_ws.txt
12.34
45.67
89.01

cut‘s field 2 for the first and third rows is the empty string between the first two of several consecutive spaces — not the number. cut is the right tool for genuinely fixed-delimiter data (a real CSV: cut -d ',' -f2 data.csv works fine, verified below); reach for awk the moment field widths or spacing can vary. In its own scratch directory, with no network and no root, a script runs the filter, comparison, locale and cut examples from this page, and skips the LC_NUMERIC test when de_DE.UTF-8 is not installed.

For the loop and quoting mistakes that show up in the scripts wrapping these one-liners, see how to write a bash script.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.