Skip to content
Advertisement

How do I tell awk not to make any changes to an empty row

I have a dataset with three columns that look like this:

    0.1 0.2 0.3
    0.4 0.5 0.6

    0.7 0.8 0.9
    1.0 1.0 1.2

I want to subtract a certain number from each entry in the second column and I know how to do that. But with how the data looks, I end up getting an output that looks like this:

    0.1 0.1 0.3
    0.4 0.4 0.6
       -0.1
    0.7 0.7 0.9
    1.0 0.9 1.2

How do I prevent awk from adding that extra “-0.1” in the empty space? I tried to use the sed command to remove that specific entry but since somewhere in the data, I have “-0.1” which must be in the data, it removes some important aspect of that data.

Advertisement

Answer

awk 'NF>0 {$2-=0.1}1' file

NF: The number of fields in the current input record.

Output:

0.1 0.1 0.3
0.4 0.4 0.6

0.7 0.7 0.9
1.0 0.9 1.2

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement