![]() |
count lines in bash script
Hi,
I am new in bash scripting and I would like to help me if you could. I have to create a bash script which find all tha files that their names match a pattern. For example *.h and then print the number of lines of those files. Any ideas???? Thanking you in advance P.S. : sORRY FOR MY ENGLISH |
pipe to "word count" with the "l" option
find [your options here] | wc -l |
Thanks for the post .
I try : find *.h | wc -l but the result is the numbers of files that have name .h I would like the number of lines in these files . . . Thanks a lot again |
You want to pipe the results of the find as command-line parameters to wc, not pass the data in a pipeline.
find *.h -print0 | xargs -0 wc -l |
It's what I need.
Thank you very much |
well, if you wanted access to your script it a gui text editor you could always pipe it out to a text editor, and a free text editor like text wrangler displays how many lines are being used.
http://www.barebones.com/products/textwrangler/ so for example you could do this Code:
sh /path/to/script | open -f -a "textwrangler" |
Quote:
(felt quite tired yesterday) |
If I would like to appear only the total lines without printing the name of files and every number of lines how could I do this ?
|
Quote:
total line count of all files that match? Period? awk 'END { print NR }' *.h would be one way (of perhaps dozens). |
Quote:
# Tony |
I use grep to display the total lines
But I when I want to display the total lines of c files, I take as error Arg list too long Does anyone know to solve my prolem ?? |
Quote:
|
find *.h -print0 | xargs -0 wc -l | grep total
|
Quote:
Code:
find *.h -print0 | xargs -0 wc -lCode:
# find /usr/include -name "*.h" -print0 | xargs -0 wc -l eg Code:
# find /usr/include -name "*.h" -print0 | xargs -0 wc -l | awk '{ total+=$1 }END{print total}' |
Display ONLY the line count!
Ciao everybody.
In continuation to the discussion, I have a further question. Using the previous discussion, I could get the following info. $ find *.sh -print0 | xargs -0 wc -l 89 cron_script.sh 159 cron_submit.sh 184 lee_matmult.sh 82 lee.sh 19 mat_mult.sh 90 newscript.sh 52 post_op.sh 5 read.sh 224 script_submit.sh 325 trial.sh 238 tut.sh 1467 total $ I want to get only the numbers, say like this 89 159 184 52 ... etc. How the numbers are displayed, does not matter. It will suffice if each number gets printed on a separate line. Could anybody help, please! |
If you want the first "column", where columns are separated from each other by a space, pipe it through: cut -d' ' -f1
|
Quote:
Code:
find /usr/include -name "*.h" -print0 | xargs -0 wc -l | awk '{ print $1}' |
For what it's worth, the wc -l command does not count lines that begin with #
|
Quote:
E.g. show us an example of a text file with a line beginning with # and then show us the results of 'wc -l' on that file. What you said is certainly not true in general (of 'wc') so either you have some weird version of 'wc' or you have misinterpreted something. |
2 Attachment(s)
Hmm, maybe it's only omitting the first line because it's calling the bash shell in these files. :confused:
|
Quote:
I.e. it's nothing to do with the # character. If you don't understand what I mean, look at the contents of those files using something like 'hexdump'. You can usually configure your editor to always have a new-line at the end of a text file (that you create with that editor) - this is a good idea. |
| All times are GMT -5. The time now is 05:41 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.