Input Data: F_Data_File.txt
1001,A1,10,USA,Y
1002,A2,10,GBR,Y
1003,A3,20,,Y
1004,A4,30,,Y
1005,A5,40,IND,Y
Desired Data:
1001,A1,10,USA,Y
1002,A2,10,GBR,Y
1003,A3,20,NA,Y
1004,A4,30,NA,Y
1005,A5,40,IND,Y
Solution:
$ awk 'BEGIN {FS=OFS=","} { if (!length($4)){$4 = "NA"} print; }' F_Data_File.txt
Explanation:
1. Length function returns the length of the field, if field length is 0 it will return FALSE and if it is <> 0 then TRUE.
2. Here we have applied the negation ( to avoide else block) , i.e. If length of the field is <> 0 it will return FALSE and when length of the field is 0 then it will return TRUE.
1001,A1,10,USA,Y
1002,A2,10,GBR,Y
1003,A3,20,,Y
1004,A4,30,,Y
1005,A5,40,IND,Y
Desired Data:
1001,A1,10,USA,Y
1002,A2,10,GBR,Y
1003,A3,20,NA,Y
1004,A4,30,NA,Y
1005,A5,40,IND,Y
Solution:
$ awk 'BEGIN {FS=OFS=","} { if (!length($4)){$4 = "NA"} print; }' F_Data_File.txt
Explanation:
1. Length function returns the length of the field, if field length is 0 it will return FALSE and if it is <> 0 then TRUE.
2. Here we have applied the negation ( to avoide else block) , i.e. If length of the field is <> 0 it will return FALSE and when length of the field is 0 then it will return TRUE.
No comments:
Post a Comment