Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I execute a script where I initialize a variable and it effects the output. The script does a recursive search thru a directory list all files less than 32 bytes. This works. I then wanted to add a counter to sum up all the bytes. When I initialize a byte counter to "0" a directory is allowed to be processed as output data. If I set the byte counter to "1", the output is as expected. The counter is off by 1 byte and the directory is not listed.
Code:
ls -lR /home 2> /dev/null | awk '
BEGIN{print "LIST OF FILES WITH LESS THEN 32 BYTES", bytes=0}
NF == 9 && $1 !~ /^d/ && $5 < 32, bytes=bytes+$5
END{print "Total " bytes " bytes"}' > ~/Chapter7/source/awk_data4
Code:
-rw-rw-r-- 1 linux1 linux1 0 Feb 20 20:01 accounta
-rw-rw-r-- 1 linux1 linux1 0 Feb 20 20:01 accountb
-rw-rw-r-- 1 linux1 linux1 0 Feb 20 20:01 accountc
drwxrwxr-x 2 linux1 linux1 4096 Jan 29 13:37 Chapter1
Also, when I initialize the variable it is printed whit the output. How do you suppress that behavior?
Thank you for your reply,
1 - Without the comma I received a syntax error. However, this is not a problem now.
I moved this after "awk" and before the BEGIN.
Quote:
ls -lR /home 2> /dev/null | awk -v counter=0 '
2 - If the bytes counter is not initialized I still get the one directory listed in
the output.
3- If I remove everything after the END then I receive a syntax error.
Quote:
awk: cmd. line:4: END blocks must have an action part
Hence you need to go and look at more examples. Specifically you need to look up what character to use to delimit one section from another ... I can tell you it is not a comma
The above reads, if there are 9 fields in the line and the first character of the first field is not 'd' and the fifth field is less than 32. . . so far so good. Now lets assume the first
file(s) you find are all of 0 bytes in length. As the counter is set to 0 initially, each of these will make the total for counter equal to 0, which is regarded as false, hence none of
these files will be printed and yet they all should be ... ie. this is not what you wanted.
I am also still surprised that having no delimiter between $5 and END doesn't cause an issue, but I do believe it then does not show the correct results.
Try creating a small subset in a single directory and test your results.
Hello grail, thank you for catching the omissions. I am still thinking about that one. I understand why it happens but have not though of the solution. Well, I could cheat! Anyway, I am curious about your comment about the delimiter. If I remove the counter then output is correct. Are you referring to parentheses; ()?
Hmmm ... turns out I have not read the doco in a while either Previously you needed a semi-colon between items not delimited by curly brackets. This would still be required
if you placed all of the command on a single line, but not with the new lines in the places you have them. The semi-colon would have been your solution though to your first print issue
and it would have told the print command to stop and then setting your variable would not have been included in the output.
You will need to incorporate curly braces into the main part of your script so as to not worry about the zero values causing incorrect output.
Hello grail, thank you for responding. I apologize but I am not quite understanding the explanation. When you state all the command are you stating all 4 lines on only 1 line? Then a semi-colon before the increment of the counter? Also, I have tried curly brace everywhere but I always get syntax errors.
Well, I had tried this and the listing for the files was suppressed. Then I thought, if one line provides the file listing and the other the counter then why not use both! That is what I did. Thanks grail, I hope this is correct!
Hello grail, once again thank you. I had tried the curly braces around the counter and I would not get any file listings. If I left the curly braces off then I would get the file listing but lose the zero byte file listings until the counter became positive. The print $0 resolved these two issues. I never saw or used awk before this past Sunday so this was a great learning exercise. Thank you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.