Remove Trailing Zeros Using Linux awk

In this tutorial, we’ll explore several methods to remove trailing zeros from your data using awk in Linux.

We’ll cover basic removal, handling scientific notation, and cleaning up tabular and comma-separated values.

 

 

Basic Removal of Trailing Zeros

Imagine you have a file, data.txt, with the following content:

123.4500
004.200
78.60000
900.000

To remove the trailing zeros, you can replace the trailing zeros in each line with an empty string :

awk '{sub(/[0]+$/, "", $1); sub(/\.$/, "", $1); print $1}' data.txt

Output:

123.45
004.2
78.6
900

This command uses awk sub function for replacement.

The second sub(/\.$/, "", $1) statement removes the trailing decimal point if there are no digits after it.

 

Handle Scientific Notation

Suppose your file scientific_data.txt contains numbers in scientific notation:

1.23000e+03
2.400e+02
3.50000e+05
4.000e+00

To remove trailing zeros in scientific notation, use this awk command:

awk '{printf "%.1e\n", $1}' scientific_data.txt

Output:

1.2e+03
2.4e+02
3.5e+05
4.0e+00

This command uses printf within awk to format the numbers. It rounds to one decimal place in scientific notation.

 

Removing Zeros from Tabular Data

Imagine you have a file named tabular_data.txt that looks like this:

ID    Value    Quantity
101   200.00   15.600
102   150.30   14.000
103   300.25   12.50000
104   400.00   10.0

To remove trailing zeros in the numerical columns, you can use awk sub function to replace the trailing zero with an empty string:

awk '{sub(/[0]+$/, "", $2); sub(/\.$/, "", $2); sub(/[0]+$/, "", $3); sub(/\.$/, "", $3); print $1, $2, $3}' data.txt

Output:

ID Value Quantity
101 200 15.6
102 150.3 14
103 300.25 12.5
104 400 10

 

Removing Zeros from CSV

Suppose you have a CSV file data.csv with the following content:

101,123.4500,200.00
102,004.200,150.30
103,78.60000,300.25
104,900.000,400.00

To remove trailing zeros from this CSV data, use this awk command:

awk 'BEGIN {FS=OFS=","} {sub(/[0]+$/, "", $2); sub(/\.$/, "", $2); sub(/[0]+$/, "", $3); sub(/\.$/, "", $3); print $1, $2, $3}' data.txt 

Output:

101,123.45,200
102,004.2,150.3
103,78.6,300.25
104,900,400

This command substitutes the trailing zeros with empty strings for the second and third fields using sub function.

Leave a Reply

Your email address will not be published. Required fields are marked *