1

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

2
  • Do you want per file count OR a total count? Commented 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 characters Commented Nov 22, 2013 at 17:50

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

7 Comments

nice idea, but you're also counting a lot of newlines. add tr -d '\n' before wc
@glennjackman: Thanks, Very good point, I was trying to strip \n in grep itself but using tr simplifies it.
but the pathway to the directory is an argument $1.How do I send it?
@user3022764: Try this: egrep -ro '[a-zA-Z0-9]+' "$1" |tr -d '\n'|wc -m
Note that the character class [[:alnum:]] is equivalent to [a-zA-Z0-9] (in the C locale) and might be easier on the eyes
|
0

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

i need an onliner with pipes and > i think
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
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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.