I have a file test.txt as below:



I want to update the value of 4th columns based on the value of 3rd column. If the value of 4th column is N replace the 3rd column value with NA and if the value of 4th column is Y then replace the 3rd column with UPDATE.

Output should be as below:


awk -F ';;;;' -v 'OFS=;;;;' '$4 == "Y" { $3 = "UPDATE" } $4 == "N" { $3 = "NA" } { print; }' test.txt

with test.txt as specified in the question prints


As stated in the question I assume you really have only 4 columns separated by ;;;; (4 semicolons), not 13 columns separated by ; (single semicolon). So this script will not work if you have lines with additional data between the groups of 4 ;.


-F ';;;;' set input field separator
-v 'OFS=;;;;' set output field separator
$4 == "Y" condition: if field 4 is Y
{ $3 = "UPDATE" } action: modify field 3
the same for N and NA
{ print; } unconditional action: print (resulting) line


With awk, first set the input n output field separators, then look for that record in the input data that has a y or an n only, then do the changes, and finally print to stdout.

$ awk -F';;;;' '
    $4 ~ /^[NY]$/{$3 = ($4 == "N") ? "NA" : "UPDATE"}1
' file

Another approach can be:

$ awk -F';;;;' 'BEGIN{OFS=FS;a["N"]="NA";a["Y"]="UPDATE"}
  $4 ~ /^[NY]$/{$3 = a[$4]}1' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.