linux - Subtract a constant number from a column -
i have 2 large files (~10gb) follows:
file1.csv
name,id,dob,year,age,score mike,1,2014-01-01,2016,2,20 ellen,2, 2012-01-01,2016,4,35 . .
file2.csv
id,course_name,course_id 1,math,101 1,physics,102 1,chemistry,103 2,math,101 2,physics,102 2,chemistry,103 . .
i want subtract 1 "id" columns of these files:
file1_updated.csv
name,id,dob,year,age,score mike,0,2014-01-01,2016,2,20 ellen,0, 2012-01-01,2016,4,35
file2_updated.csv
id,course_name,course_id 0,math,101 0,physics,102 0,chemistry,103 1,math,101 1,physics,102 1,chemistry,103
i have tried awk '{print ($1 - 1) "," $0}' file2.csv
, did not correct result:
-1,id,course_name,course_id 0,1,math,101 0,1,physics,102 0,1,chemistry,103 1,2,math,101 1,2,physics,102 1,2,chemistry,103
you've added column in attempt. instead set first field $1
$1-1
:
awk -f"," 'begin{ofs=","} {$1=$1-1;print $0}' file2.csv
that semicolon separates commands. set delimiter comma (-f","
) , output field seperator comma begin{ofs=","}
. first command subtract 1 first field executes first, print command executes second, entire record, $0
, contain new $1
value when it's printed.
it might helpful subtract 1 records not header. can add condition first command:
awk -f"," 'begin{ofs=","} nr>1{$1=$1-1} {print $0}' file2.csv
now subtract when record number (nr
) greater 1. print entire record.
Comments
Post a Comment