LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   printf doesn't work in awk (https://www.linuxquestions.org/questions/programming-9/printf-doesnt-work-in-awk-671528/)

MheAd 09-22-2008 04:34 AM

printf doesn't work in awk
 
Hi guys.
I'm writing this awk-script and I'm getting problems in the BEGIN-part of it. I'll not post the entire script but I'm going to isolate the problem.

Let's say I want to pipe ls -l command with awk and want to print 2 fields.

I would do something like:

Code:

ls -l | awk '{ print $5, $8 }'
To get a nicer format, I could do:

Code:

ls -l | awk '{ printf "%-20s\t%-20s\n", $5, $8 }'
I want to add then a BEGIN part with two fields formated after the same pattern:

Code:

ls -l | awk '
BEGIN { printf "%-20s\t%-20s\n", BYTES FILE }
      { printf "%-20s\t%-20s\n", $5, $8 }'

Awk is giving me following error:

Code:

awk: cmd. line:1: fatal: not enough arguments to satisfy format string
`%-20s  %-20s'
        ^ ran out for this one

I don't understand. If I type a single printf - command in my shell with formatting above it prints words BYTES and FILE aligned according to specifications and separated by tabs. With other words - the printf command itself is not typed in wrong way.

What's the deal?
Thanks in advance!
M.

vladmihaisima 09-22-2008 04:51 AM

You miss a comma between BYTES and FILE.

Edit:

Code:

ls -l | awk '
BEGIN { printf "%-20s\t%-20s\n", "BYTES", "FILE" }
      { printf "%-20s\t%-20s\n", $5, $8 }'


MheAd 09-22-2008 04:59 AM

Heh...thanks

Not only I missed the comma but even quotes ("") between the words.
So, awk probably interpreted these two words as variables and not words...
I'm still getting used to this tool...
Now it works like a charm! Thanks a lot!
:)

EDIT:

However, I've noticed another problem. Sometimes the file names are containing the spaces, so when printing the 8th field I only get a part of the file name, before the first space. Any way to make all of the words in the file name end up as field 8, perhaps changing FS during the loop itself? I've tried some odd combinations without success. My friend would laugh at me and say "This can be done in 5 seconds with Perl". Oh well...I don't know anything about Perl just yet :)

ghostdog74 09-22-2008 05:43 AM

use GNU find if you have it
Code:

printf "%-20s\t%-20s\n", "BYTES", "FILE"
find -maxdepth 1 -printf  "%-20s\t%-20f\n"


archtoad6 09-24-2008 01:46 PM

Great use of find, but apparently when you use the stand alone printf command, you don't need the commas:
Code:

# printf "%-20s\t%-20s\n", "BYTES", "FILE"
BYTES,                  FILE

# printf "%-20s\t%-20s\n" "BYTES" "FILE"
BYTES                  FILE

And I wouldn't left justify the BYTES field:
Code:

printf "%20s\t%-20s\n" "BYTES" "FILE"
find -maxdepth 1 -printf  "%20s\t%-20f\n"


Unfortunately, I like my numbers readable -- i.e. w/ thousands separators included. gawk can add these. du supplies exactly the 2 fields we want. Combining these:
Code:

du --max-depth 1 -b * |\
gawk -Ft \
'BEGIN {printf "%12s  %-s\n","bytes","file"};\
      {printf "%'\''12d  %-s\n",$1,$2}'

Note: I have spent the better part of an hour debugging this -- the problem is that the gawk "-Ft" option is not working on my system today. If the same happens to you, change "-Ft" to "-F'<TAB>'" (put an actual tab in the command). Arrrgh!


All times are GMT -5. The time now is 04:56 PM.