Question: I am trying to print 'NA' if the output columns are empty, like

```sh
$cat file.txt
111||aa1|11a|key
|bbb|aa2|22a|key
333|ccc||33a|key
444|ddd||44a|key
555|eee|aa5|55a|key

```

If I do awk I am getting like 

```sh
$awk -F'|' '{print $1,$2,$3}' file.txt
111  aa1
 bbb aa2
333 ccc 
444 ddd 
555 eee aa5

```

But I want the empty columns to be filled with 'NA' like below

```sh
111 NA aa1
NA bbb aa2
333 ccc NA 
444 ddd NA
555 eee aa5

```

tried: 

```sh
$awk -F'|' '{print '!$1 $1="NA"}',$2,$3}' file.txt 
awk -F'|' '{print $1,if($2=="") $2="NA",$3}' file.txt

```

Please help me in filling "NA" if the field is empty

Answer: 
You may use this `awk`:

```
awk -F '|' '{sub(/^\|/, "NA|"); gsub(/\|\|/, "|NA|"); print $1, $2, $3}' file

```

```
111 NA aa1
NA bbb aa2
333 ccc NA
444 ddd NA
555 eee aa5

```

another variant:

```
awk -F '|' '{for(i=1; i<=3; i++) if ($i=="") $i="NA"; print $1, $2, $3}' file

```

```
111 NA aa1
NA bbb aa2
333 ccc NA
444 ddd NA
555 eee aa5

```
I don't think there is an idiomatic way to do it, but you could use the ternary operator to shorten it a bit:

```
awk -F'|' '{n="NA";print ($1!=""?$1:n),($2!=""?$2:n),($3!=""?$3:n)}' file.txt

```

Output:

```
111 NA aa1
NA bbb aa2
333 ccc NA
444 ddd NA
555 eee aa5
```