Replace null values with dot using awk

Using awk I am trying to replace all blank or null values with a . in the tad delimited input . I hope the awk is close. Thank you :).

input

name    test
sam      1

liz         2
al
             1

awk

awk 'BEGIN{FS=OFS="\t"}{for(i=1;++i<NF;)$i=$i?$i:"."}1'input
awk 'BEGIN { FS = OFS = "\t" } { for(i=1; i<=NF; i++) if($i ~ /^ *$/) $i = 0 }; 1' input

desired output

name    test
sam      1
.            .
liz         2
al          .
.            1

---------- Post updated at 11:18 AM ---------- Previous update was at 09:48 AM ----------

This awk seems to work:

awk -F"\t" '{ for (i=1;i<=NF;i++) { if ($i=="") { $i="." } } OFS="\t";print }' file

Thank you :).

Hello cmccabe,

You haven't told us if you want to keep the same spaces(as shown in Input_file) into your post or not. Considering if you have only 2 fields and you want to maintain the spaces as it is(though I tried hard to keep the space same), following may help you in same.

awk -F"[[:space:]]+" 'function quick(Q){match(Q,/[[:space:]]+/);W=substr(Q,RSTART,RLENGTH);return W} NF==1{print $1 quick(Q) ".";Q=$0;next} NF==0{print "." quick(Q) ".";Q=$0;next} NF==2{if($0 ~ /^[[:space:]]+/){print "." quick($0) $2} else {print}}{Q=$0}'  Input_file
 

Output will be as follows.

name    test
sam      1
.      .
liz         2
al         .
.             1

If your requirements are different than the shown post, please post complete details and expected output too into your next post.
EDIT: Adding non-one liner form of solution now on same.

awk -F"[[:space:]]+" 'function quick(Q){
                                        match(Q,/[[:space:]]+/);
                                        W=substr(Q,RSTART,RLENGTH);
                                        return W
                                       }
                      NF==1            {
                                        print $1 quick(Q) ".";
                                        Q=$0;
                                        next
                                       }
                      NF==0            {
                                        print "." quick(Q) ".";
                                        Q=$0;
                                        next
                                       }
                      NF==2            {
                                        if($0 ~ /^[[:space:]]+/){
                                                                        print "." quick($0) $2
                                                                }
                                        else                    {
                                                                        print
                                                                }
                                       }
                                       {
                                        Q=$0
                                       }
                      '   Input_file
 

Thanks,
R. Singh

sed 's/^ *\t/.\t/; s/\t *\t/\t.\t/g; s/\t$/\t./' input

Hello cmccabe,

Seems you have edited your post, so if you are trying to get number of fields from very first line(as it may be heading) and trying to put values of those fields which are having less number of fields as compare to heading, then following could do.
Let's see following is Input_file:

ame    test  test1  test2  test3  test4  test5
sam      1
liz         2
al
            1

Then following is the one.

awk 'NR==1{Q=NF;print} NR>1{for(i=1;i<=Q;i++){if(!$i){$i="."}};print}'  Input_file

Output will be as follows.

name    test  test1  test2  test3  test4  test5
sam 1 . . . . .
. . . . . . .
liz 2 . . . . .
al . . . . . .
1 . . . . . .

As you could see number of fields set by very first line to 7, similarly you could set number of fields too in case you don't want to take them from heading(very first line) too.

Thanks,
R. Singh

Thank you very much :).

That will work as long as:

  1. you never have two adjacent fields only containing zero or more <space> characters (other than in a line only containing one <tab>),
  2. you never have only one or more <space> characters in the last field on an input line,
  3. you are using a version of sed that allows you to separate substitute commands with a <semicolon> character,
  4. you are using a version of the sed utility that recognizes \t in an RE as a <tab> character, and
  5. you are using a version of the sed utility that recognizes \t in a replacement string as a <tab> character

(all of the last three of which produce behavior that is not specified the standards).

The first problem can be fixed by repeating the 2nd substitute command:

sed 's/^ *\t/.\t/; s/\t *\t/\t.\t/g; s/\t *\t/\t.\t/g; s/\t$/\t./' input

The second problem can be fixed by adding a * to the RE in the last substitute command:

sed 's/^ *\t/.\t/; s/\t *\t/\t.\t/g; s/\t *\t/\t.\t/g; s/\t *$/\t./' input

The third problem can be fixed by replacing the <semicolon> characters with <newline> characters:

sed 's/^ *\t/.\t/
 s/\t *\t/\t.\t/g
 s/\t *\t/\t.\t/g
 s/\t *$/\t./' input

And the last two problems can be fixed by replacing every occurrence of \t in the above sed command with a literal <tab> character.

Your 2nd solution is better structured than your working solution.

awk 'BEGIN { FS=OFS="\t" } { for (i=1; i<=NF; i++) if ($i~/^ *$/) $i="." } 1' input

It treats space characters as being empty, unlike your last solution that checks for being empty

awk 'BEGIN { FS=OFS="\t" } { for (i=1; i<=NF; i++) if ($i=="") $i="." } 1' input

---------- Post updated at 15:45 ---------- Previous update was at 15:12 ----------

Don's sed solution, after replacing \t with ${T} and putting the code in double-quotes, so the shell can substitute each ${T} .
For general safety I put the braces and escaped the $ (both not really required here).

T=$'\t'; sed "
s/^ *${T}/.${T}/
s/${T} *${T}/${T}.${T}/g
s/${T} *${T}/${T}.${T}/g
s/${T} *\$/${T}./
" input