![]() |
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 }'Code:
ls -l | awk '{ printf "%-20s\t%-20s\n", $5, $8 }'Code:
ls -l | awk 'Code:
awk: cmd. line:1: fatal: not enough arguments to satisfy format stringWhat's the deal? Thanks in advance! M. |
You miss a comma between BYTES and FILE.
Edit: Code:
ls -l | awk ' |
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 :) |
use GNU find if you have it
Code:
printf "%-20s\t%-20s\n", "BYTES", "FILE" |
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"Code:
printf "%20s\t%-20s\n" "BYTES" "FILE" 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 * |\ |
| All times are GMT -5. The time now is 04:56 PM. |