The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   count lines in bash script (http://hintsforums.macworld.com/showthread.php?t=72327)

prokopis 05-14-2007 11:02 AM

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

Hal Itosis 05-14-2007 12:33 PM

pipe to "word count" with the "l" option

find [your options here] | wc -l

prokopis 05-14-2007 12:55 PM

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

dmacks 05-14-2007 01:40 PM

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

prokopis 05-14-2007 02:08 PM

It's what I need.

Thank you very much

tlarkin 05-14-2007 03:53 PM

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"
This will output the script into a text editor of your choice, look through the man pages of open, which is a very nice utility in OS X.

Hal Itosis 05-15-2007 02:04 PM

Quote:

Originally Posted by dmacks (Post 379077)
find *.h -print0 | xargs -0 wc -l

That's what I meant to post.
(felt quite tired yesterday)

prokopis 05-18-2007 01:31 PM

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 ?

Hal Itosis 05-18-2007 10:18 PM

Quote:

Originally Posted by prokopis (Post 380112)
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 ?

Not sure I understand... you want the
total line count of all files that match?
Period?

awk 'END { print NR }' *.h

would be one way (of perhaps dozens).

honestpuck 05-19-2007 12:31 AM

Quote:

Originally Posted by Hal Itosis (Post 380212)
Not sure I understand... you want the
total line count of all files that match?
Period?

awk 'END { print NR }' *.h

would be one way (of perhaps dozens).

Though certainly one of the more elegant solutions. :)

# Tony

prokopis 05-19-2007 04:02 AM

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 ??

ghostdog74 05-19-2007 04:30 AM

Quote:

Originally Posted by prokopis (Post 380242)
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 ??

in your previous posts, you had used find and said it works, now you are using grep. can you show what you have coded?

prokopis 05-19-2007 06:13 AM

find *.h -print0 | xargs -0 wc -l | grep total

ghostdog74 05-19-2007 06:26 AM

Quote:

Originally Posted by prokopis (Post 380259)
find *.h -print0 | xargs -0 wc -l | grep total

this statement
Code:

find *.h -print0 | xargs -0 wc -l
gives you line counts of every individual *.h found. A sample from my system:
Code:

# find /usr/include -name "*.h" -print0 | xargs -0 wc -l
    115 /usr/include/expat_external.h
    1013 /usr/include/expat.h
    195 /usr/include/GL/GLwDrawA.h
    250 /usr/include/GL/gle.h
      41 /usr/include/GL/GLwMDrawAP.h
    130 /usr/include/GL/GLwDrawAP.h

.....

therefore, if i want to find the total count, of ALL files found..I can total up the first column..
eg
Code:

# find /usr/include -name "*.h" -print0 | xargs -0 wc -l | awk '{ total+=$1 }END{print total}'
1512320

this gives you total number of lines of all *.h files. If you want, pls show a sample of your find *.h -print0 | xargs -0 wc -l command...

kvaibhav 05-31-2007 02:14 PM

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!

dmacks 05-31-2007 02:53 PM

If you want the first "column", where columns are separated from each other by a space, pipe it through: cut -d' ' -f1

ghostdog74 06-01-2007 01:45 AM

Quote:

Originally Posted by kvaibhav (Post 382741)
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!

Code:

find /usr/include -name "*.h" -print0 | xargs -0 wc -l | awk '{ print $1}'

cwtnospam 06-20-2007 01:20 PM

For what it's worth, the wc -l command does not count lines that begin with #

hayne 06-20-2007 02:27 PM

Quote:

Originally Posted by cwtnospam (Post 387283)
For what it's worth, the wc -l command does not count lines that begin with #

Please explain where you drew this conclusion from.
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.

cwtnospam 06-20-2007 03:41 PM

2 Attachment(s)
Hmm, maybe it's only omitting the first line because it's calling the bash shell in these files. :confused:

hayne 06-20-2007 04:32 PM

Quote:

Originally Posted by cwtnospam (Post 387319)
Hmm, maybe it's only omitting the first line because it's calling the bash shell in these files

I think the problem is that there is no end-of-line character at the end of the last line in those text files. I think 'wc -l' merely counts the number of new-line characters in the file.
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.