Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-22-2008, 05:34 AM   #1
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

Rep: Reputation: 36
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:

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

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:

ls -l | awk '
BEGIN { printf "%-20s\t%-20s\n", BYTES FILE }
      { printf "%-20s\t%-20s\n", $5, $8 }'
Awk is giving me following error:

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!

Last edited by MheAd; 09-22-2008 at 05:38 AM.
Old 09-22-2008, 05:51 AM   #2
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
You miss a comma between BYTES and FILE.


ls -l | awk '
BEGIN { printf "%-20s\t%-20s\n", "BYTES", "FILE" }
      { printf "%-20s\t%-20s\n", $5, $8 }'
Old 09-22-2008, 05:59 AM   #3
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

Original Poster
Rep: Reputation: 36

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!


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

Last edited by MheAd; 09-22-2008 at 06:05 AM.
Old 09-22-2008, 06:43 AM   #4
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use GNU find if you have it
printf "%-20s\t%-20s\n", "BYTES", "FILE" 
find -maxdepth 1 -printf  "%-20s\t%-20f\n"
Old 09-24-2008, 02:46 PM   #5
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Great use of find, but apparently when you use the stand alone printf command, you don't need the commas:
# 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:
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:
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!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
why is doesnt work? (awk, set) DoME69 Programming 4 09-21-2008 07:32 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
Minus sign in printf format string doesn't work. zebela Linux - Newbie 4 06-13-2008 02:32 AM
How does printf work? Ephracis Programming 1 12-25-2004 04:06 AM
need help w/ awk & printf rickenbacherus Programming 3 05-13-2004 08:21 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:55 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration