Skip to content
Advertisement

Subtract a constant number from a column

I have two large files (~10GB) as follows:

file1.csv

JavaScript

file2.csv

JavaScript

I want to subtract 1 from the “id” columns of these files:

file1_updated.csv

JavaScript

file2_updated.csv

JavaScript

I have tried awk '{print ($1 - 1) "," $0}' file2.csv, but did not get the correct result:

JavaScript

Advertisement

Answer

You’ve added an extra column in your attempt. Instead set your first field $1 to $1-1:

JavaScript

That semicolon separates the commands. We set the delimiter to comma (-F",") and the Output Field Seperator to comma BEGIN{OFS=","}. The first command to subtract 1 from the first field executes first, then the print command executes second, so the entire record, $0, will now contain the new $1 value when it’s printed.

It might be helpful to only subtract 1 from records that are not your header. So you can add a condition to the first command:

JavaScript

Now we only subtract when the record number (NR) is greater than 1. Then we just print the entire record.

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