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

Number: 1.0.2.66

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

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

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

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

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

See also  How to Create Thousands/Millions Files in Linux

Leave a Comment