awk change one value in csv based on another value in the record

I've found 2 great discussions on this forum that are tied to my question, but for some reason, neither solution works for me.

I have a CSV file with many records, up to 150+ at some times. Many records have a value of H in $1. For those records, I need to add today's current date in $20. I'm not concerned about the date yet, I'm just trying to populate $20 with some value, any value.

I've tried ...

awk 'BEGIN {FS = ","} {if ($1 == H) {$20 = 20110801}}' MA540CSV
  • and all I get is the contents of the data file MA540CSV scroll across my Unix screen.

I've also tried ...

awk 'BEGIN {OFS=","} {if ($1=="H") sub($20,"20110801"); print }' MA540CSV

Result, nothing. My command returns me to the command line, with no changes.

Any help would be greatly appreciated. If you need me to show a few records here, please let me know.

In order to give a sensible answer we need to see a sample of your input file and an example of the expected output.

1 Like

Hi,

I will make a try (without testing), althought should be much better with sample input and output as Radoulov pointed out.

$ awk 'BEGIN {FS = OFS = ","} {if ($1 == "H") {$4 = 20110801}; print}' MA540CSV

Regards,
Birei

1 Like

No problem folks ... thanks for helping out ...

Sample Input

H,0004,3727,OWENS810,1079659,,,,,IC,4014527,,,,,,,,20110630,,P4,,7.40,,,,,,,,,,,,,,,EDI,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0004,3727,OWENS810,1079659,,1,,,IC,4014527,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,001,17353,6504402300,2.0000,BX,3.70000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,WIPE REM ADH UNI SOLVE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
H,0013,3727,OWENS810,1079643,,,,,IC,13016351,,,,,,,,20110630,,P13,,269.36,,,,,,,,,,,,,,,EDI,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0013,3727,OWENS810,1079643,,1,,,IC,13016351,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,001,116296,191689739101,2.0000,CA,117.68000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,PACK EXTREMITY,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0013,3727,OWENS810,1079643,,2,,,IC,13016351,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,002,5,4509001015,1.0000,BX,34.00000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,DRAPE SURGICAL STERI U,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
H,0014,3727,OWENS810,1079489,,,,,,14057439,,,,,,,,20110630,,P14,,59.84,,,,,,,,,,,,,,,EDI,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0014,3727,OWENS810,1079489,,1,,,,14057439,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,001,10595,4352MDS9380,4.0000,EA,9.86000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,MDS9380 CUFF B/P STANDARD,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0014,3727,OWENS810,1079489,,2,,,,14057439,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,002,78534,4352MDS9388,1.0000,EA,10.80000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,CUFF ANEROID OBESE OBESE LATEX,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0014,3727,OWENS810,1079489,,3,,,,14057439,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,003,65819,4352MDS9387,1.0000,EA,9.60000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,MDS9387 CUFF B/P CHILDS,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Sample Output

H,0004,3727,OWENS810,1079659,,,,,IC,4014527,,,,,,,,20110630,20110801,P4,,7.40,,,,,,,,,,,,,,,EDI,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0004,3727,OWENS810,1079659,,1,,,IC,4014527,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,001,17353,6504402300,2.0000,BX,3.70000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,WIPE REM ADH UNI SOLVE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
H,0013,3727,OWENS810,1079643,,,,,IC,13016351,,,,,,,,20110630,20110801,P13,,269.36,,,,,,,,,,,,,,,EDI,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0013,3727,OWENS810,1079643,,1,,,IC,13016351,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,001,116296,191689739101,2.0000,CA,117.68000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,PACK EXTREMITY,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0013,3727,OWENS810,1079643,,2,,,IC,13016351,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,002,5,4509001015,1.0000,BX,34.00000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,DRAPE SURGICAL STERI U,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
H,0014,3727,OWENS810,1079489,,,,,,14057439,,,,,,,,20110630,20110801,P14,,59.84,,,,,,,,,,,,,,,EDI,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0014,3727,OWENS810,1079489,,1,,,,14057439,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,001,10595,4352MDS9380,4.0000,EA,9.86000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,MDS9380 CUFF B/P STANDARD,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0014,3727,OWENS810,1079489,,2,,,,14057439,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,002,78534,4352MDS9388,1.0000,EA,10.80000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,CUFF ANEROID OBESE OBESE LATEX,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
D,0014,3727,OWENS810,1079489,,3,,,,14057439,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,003,65819,4352MDS9387,1.0000,EA,9.60000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,MDS9387 CUFF B/P CHILDS,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

---------- Post updated at 05:05 PM ---------- Previous update was at 05:02 PM ----------

Birei! Hooray! Birei! Hooray! My syntax was not quite as clean as yours. Yours worked wonderfully. Now I'll work on inserting the current date into the second value. Thanks again for all your help!

awk -F, '/^H/ { $20 = dt }1' dt="$( date '+%Y%m%d' )" OFS=, infile
1 Like