How can I find and replace value for particular column using awk?
Say for example -> I have a file test having the content:
"abc":"100"::"new"
"xyz":"200":"mob":"old"
"lmn":"300"::"new"
"pqr":"400":"mob2":"new"
Now, if 3rd column is blank then I want to replace the blank value from "N/A" otherwise print the line as it is, so that the output would be like:
"abc":"100":"N/A":"new"
"xyz":"200":"mob":"old"
"lmn":"300":"N/A":"new"
"pqr":"400":"mob2":"new"
Although I got the output using awk through below command:
awk -F":" '{
if ( $3 == "")
print $1":"$2":\"N\/A\":"$4
else
print $0
}' test
But here I am using the hard coded values for each column like $1, $2, so if the blank column changes in other example from 3rd to xyz then have to change the same in command again. Is there any other way to get the same output using awk and without using hard coded values for columns? Thanks for your help.
if the blank column changes in other example from 3rd to xyz then have to change the same in command againmean then? It sounds like you're saying with that sentence that you want to change any blank column, but then you also saythe replace should work only for those rows in which value of 3rd column is blankwhich means only the 3rd column should be tested/changed. It's very unclear....