awk: how do you ensure consistent column numbering when there are blank columns?

Viewed 35

my_file is like this:

SELECTED   NAME    AGE
*          adam    30
           bob     70

I'd like to output:

adam
bob

however, if I try: cat my_file|awk '{print $2}' it outputs

NAME
adam
70

Any suggestions on how you get awk to account for a blank column?

2 Answers

with gawk field widths

$ awk -v FIELDWIDTHS='11 8 3' '{print $2}' file

NAME
adam
bob

Could you please try following.

awk '{printf("%s%s",/^ +/?$1:$2,ORS)}'  Input_file

Output will be as follows.

NAME
adam
bob
Related