Home > Net >  how to use script to conditionally modify values in CSV file
how to use script to conditionally modify values in CSV file

Time:11-30

The CSV file contains name and Values

i want any value more than 1000 converted to 1000 in same file or in differentt file. mostly using shell script. what is the best way to it?

for example the values are as follows

Name Value
ABV 1200
CCD 1000
CAD 500
DDD 1800

and i want it as

Name Value
ABV 1000
CCD 1000
CAD 500
DDD 1000

i tried awk function but it didnt work any other alteernatives

CodePudding user response:

Using awk:

$ awk '{print $1,($2 0>1000?1000:$2)}' file

Output:

Name Value
ABV 1000
CCD 1000
CAD 500
DDD 1000

CodePudding user response:

A few issues with OP's current code:

  • need to skip processing of the first line
  • -gt is invalid in awk ... use > instead
  • -F, says to use the comma as the input field delimiter but the sample input file does not contain commas; for now I'm going to assume the sample input file is correct (ie, there are no commas)

Updating OP's current code to address these issues:

awk 'NR==1 {print $0; next} {if ($2 > 1000) {$2=1000} {print $0}}' book1.csv

Or an equivalent:

awk 'NR>1 && ($2>1000) {$2=1000} 1' book1.csv

Both of these generate:

Name Value
ABV 1000
CCD 1000
CAD 500
DDD 1000
  • Related