LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-22-2008, 05:34 AM   #1
MheAd
Member
 
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:

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.

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

Rep: Reputation: 33
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 }'
 
Old 09-22-2008, 05:59 AM   #3
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 14.04
Posts: 186

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

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

Rep: Reputation: 244Reputation: 244Reputation: 244
use GNU find if you have it
Code:
printf "%-20s\t%-20s\n", "BYTES", "FILE" 
find -maxdepth 1 -printf  "%-20s\t%-20f\n"
 
Old 09-24-2008, 02:46 PM   #5
archtoad6
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:
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!
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 PM.

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