how can I count the characters (a-z,0-9,A-Z) from files from a directory which has others subdirectories .I have tried with grep and wc but it didn't works
-
Do you want per file count OR a total count?anubhava– anubhava2013-11-22 17:48:13 +00:00Commented Nov 22, 2013 at 17:48
-
total.I have to create a scipt that counts the total number of characters from a directory that can contain another subdirectories in which can be the files with the charactersLaura– Laura2013-11-22 17:50:47 +00:00Commented Nov 22, 2013 at 17:50
Add a comment
|
2 Answers
Use this egrep:
egrep -ro '[a-zA-Z0-9]+' *|tr -d '\n'|wc -m
OR this:
egrep -ro '[[:alnum:]]+' "$1" |tr -d '\n'|wc -m
7 Comments
glenn jackman
nice idea, but you're also counting a lot of newlines. add
tr -d '\n' before wcanubhava
@glennjackman: Thanks, Very good point, I was trying to strip
\n in grep itself but using tr simplifies it.Laura
but the pathway to the directory is an argument $1.How do I send it?
anubhava
@user3022764: Try this:
egrep -ro '[a-zA-Z0-9]+' "$1" |tr -d '\n'|wc -mglenn jackman
Note that the character class
[[:alnum:]] is equivalent to [a-zA-Z0-9] (in the C locale) and might be easier on the eyes |
This is a script that when you ll run it given the full path of the main folder ( the folder that contains the sub-directories which contains the text files ) as first argument will print out the total count --->>>
## $1 will be the total path
## This variable will be
## a list of all sub-directories
sub_dirs=`ls $1`
## Total holding count variable
count=0
## Looping over the
## each sub-directory
for i in $sub_dirs;
do
## Looping over
## each file
for j in `ls $1/$i`;
do
## Counting the characters
total_characters=`wc -c "$1/$i/$j" | awk ' { print $1 } '`;
## Adding the result to the count
count=`expr $count + $total_characters`;
done
done
## Printing out the total count
echo $count
I hope this helps...as i didnt fully understand the question...
Cheers from Greece Stay UNIX!
4 Comments
Laura
i need an onliner with pipes and > i think
Laura
i have to create an onliner in a script like I said before. it receives like argument the pathway to a directory that contains ohers directories and files.and i Have to redirect the total number of chars from the words that are in those files in a /home/student/chars .But only 0-9 and a-z ,A-Z,without space ot other special chars. In the and, in file.err the script should write the errors that appers during the execution of the script.It is a homework at OS and i has just begun shell scripting.please help.Tomorrow I have an exam
L0aD1nG
Ohh now i see...i will try this when i ll be on my city i ll soon travel their i am to an other city where my college is. Just a question you want the files to remain the same after the count?
Laura
yes.because the system will generate the files and the directories.It is a homework and I have all the inputs.SO my onliner starst like this: find $1 -type f/grep........ and the system will also generate some errors for me to redirect them in a file called .home/student/chars03.err