How to separate fields with space or tab in awk

While playing with awk I came to execute:

ls -la >> a.txt ; awk {'print $5  $1'} a.txt ;

This is giving output like:

53277-rw-------
52347-rw-------

How can I get a space between these two friends of output?


Solution 1:

Just change the line to

ls -la >> a.txt ; awk {'print $5 "        " $1'} a.txt ;

this should print the output with spaces.

Hope this helps.

Edit:

As suggested by McNisse you can use printf, which would provide you good output format

ls -la >> a.txt ; awk {'printf ("%5s\t%s\n", $5, $1)'} a.txt ;

Solution 2:

Another awk-specific technique, use the "output field separator"

ls -la | awk -v OFS='\t' '{print $5, $1}'

The comma is crucial here.