Awk: How do you build a string variable with new lines?

Viewed 1770

I am trying to do the following:

result of ls -l on a folder:

-rw-rw-r--   1 root  root  100  May 23 09:45 filename1
-rw-rw-r--   1 root  root  200  May 23 09:45 filename2
-rw-rw-r--   1 root  root  500  May 23 09:46 filename3

Now I want to pass this through awk to do the following:

800 bytes, files:
filename1
filename2
filename3

So far I can get awk to add up the bytes:

output=`ls -l /some/folder/ | awk 'START {total = 0}; {total += $5} END{print total}'`

This simple gives: 800

Now I want to start building up the output string, so I am trying to get a list of filenames (column $9 I think), I am trying like this:

output=`ls -l /some/folder/ | awk 'START {total = 0; files=""}; {total += $5 files="\n" files $9} END{print total "files:" files}'`

echo $output gives the following:

800 filename1 filename2 filename3

I want it to show:

800
filename1
filename2
filename3

I don't understand why the lines are not split into new lines?

3 Answers
Related