42

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?

  • 1
    As an advice, you should not parse the output of ls. This will bite you in the back sooner or later. – gniourf_gniourf Dec 23 '12 at 10:08
  • In awk, to concatenate two strings, you just place them side-by-side -- print $5 $1 – glenn jackman Dec 23 '12 at 12:31
  • 3
    Possible duplicate of How do I select a field/column from the output of `ls -l`? – kenorb Apr 10 '16 at 4:28
  • 2
    @kenorb and close voters, the linked duplicate is not a duplicate at all. They know already how to print fields by premise (awk {'print $5 $1'} a.txt). They're asking how to put a space in between field #1 and field #5. – kos Apr 11 '16 at 17:49
62

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 ;
  • 3
    The printf function provides better control, specially if you want to format numbers. printf("%s\t%s\n", $5, $1) – McNisse Dec 23 '12 at 10:11
60

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

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

The comma is crucial here.

  • this works but cause $1 to go to newline – loretoparisi Oct 26 '16 at 21:47
4

I know this is an old thread, but I'm just learning and found these posts helpful. My best solution was to use gawk to insert spaces between the variables for you.

ls -la | gawk '{print $1, $9}'
  • 2
    -1: Nothing new. gawk, mawk, or any other awk, they all insert spaces if you use the comma, which is what this answer says. – muru Sep 18 '14 at 23:19
  • 1
    Should be ls -la | awk '{print $1, $9}'. As muru said gawk isn't necessary. – Nick Crawford Aug 25 '15 at 16:33
3

A simple way to get tabs is:

awk {'print $5"\t"$1'}
1

To place the space between the arguments, just add " ", e.g. awk {'print $5" "$1'}.

However it is not recommended to parse output of ls command, since it's not reliable and output is for humans, not scripts. Therefore use alternative commands such as find or stat.

Here is example using GNU stat:

$ stat -t *
001.txt 23 8 81a4 501 20 1000004 242236402 1 0 0 1460260387 1460260239 1460260239 1460260194 4096
7c1c.txt 21 8 81a4 501 20 1000004 242236595 1 0 0 1460261322 1460260486 1460260486 1460260486 4096

which will print you machine-friendly output (in terse form), so you can get exactly what you need. Then use -c to use specific format, or use awk, cut or read to get the right columns.

Check stat --help for further options. For example to print day of modification, check this example.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.