ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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:
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.