Need a Linux command for find/replace column based on specific criteria.

I'm new to shell programming, I have a huge text file in the following format, where columns are separated by single space:

ACA MEX 4O_ $98.00 $127.40 $166.60 0:00 0:00 0 ;
ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;
ACA YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;
ADZ YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;
ADZ YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;
AGP AMS HV_ $441.00 $573.30 $749.70 0:00 0:00 0 ;
AGP AMS VY_ $235.00 $305.50 $399.50 0:00 0:00 0 ;

I want to replace all values in the 5th column by $-1.00 only when the third column is equal to " TS_ ". Basically the result of transforming the following line:

ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;

Would be:

ACA YUL TS_ $300.00 $-1.00 $510.00 0:00 0:00 0 ;

Is there any easy way to do it via AWK command ?

Please advise.

Try

awk '{ if ($3 == "TS_") $5 = "$-1.00" ; print }' input >output
1 Like

Or

awk '$5=($3=="TS_")?"$-1.00":$5' file

assuming $5 will never be empty nor zero.

1 Like

that works perfectly. Thank you guys.