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

number of files, linux, ubuntu, red hat

This article will show you how to parse a decimal number (such as a software release number) into individual parts. For example, you can do this if you need to compare the the minor release number of two versions. There are numerous ways to accomplish the same thing using Linux and I will show you two of them: awk and cut.

Example 1 – Using awk

Number: 52.4

echo "52.4" | awk 'BEGIN {FS="."}{print $1, $2}'
Output: 52 4

echo "52.4" | awk 'BEGIN {FS="."}{print $1}'
Output: 52

echo "52.4" | awk 'BEGIN {FS="."}{print $2}'
Output: 4

Example 2 – Using cut


echo "" | cut -d. -f1
Output: 52 4

echo "" | cut -d. -f2
Output: 0

echo "" | cut -d. -f3
Output: 2

echo "" | cut -d. -f4
Output: 66

There you have it, two different methods for parsing decimal numbers!

See also  Free Linux utility to convert PDF to Text file

Leave a Comment