Bash/AWK Newbie taking on more than he can chew.

A few questions: I'm trying to use Bash (although I'm not against using AWK) to try to accomplish a few things, but I'm stumped on a few points.

I'm learning most of the basics quickly: but there are a few things I can't figure out.

  1. I'm trying to count the number of .txt files in a directory and save it in a variable.
    I can use
    find $DIRECTORY -name *.txt | wc -l
    but can't save that in a variable. :frowning:

2&3. Is it possible to actually open these files (this will be a separate task entirely) and read a specific line? All of them are formatted the same, and I need to be able to read that line and save THAT to a list/array of variables- there are over 10,000 files- and I need the fourth line in each .txt. Many of these .txt files should have this line the same, so the array will never approach 10,000 - but I do need to be able to count how many times each appears- how would I do this? :confused:

(I know how to use loops, and create variables, but not open files (and read specific lines) nor make arrays/lists/whatever you call them in bash. <-Used to Java/Python; but can't use either here. :mad:

Any help with this is greatly appreciated- and I thank you for any help you can provide. :slight_smile:

Last bit:
the files are all named similarly... 309456_20.txt
503256_01.txt
etc.

and all the files are in subdirectories named the same as the first three digits of the file.
So 309456_20.txt would be in subdirectory 309.

I'm not looking for a fast program or even a relatively efficient one; as long as it gets the job done.

Is there no-one that can help? -Or am I trying to do something that just isn't possible?

For part 1 ...

TESTVAR=$(find . -name log_20070911* |wc -l)
echo $TESTVAR
1

If you enclose a command in $( ) then the output of that command is used, in this case, assigned to the variable TESTVAR.

You could use ` ` also.

Martin

(Parts 2/3 I need to think about)

for x in `find . -name *.txt`
do
sed -n '4p' $x
done

should get you the 4th line from each file

and then

| sort | uniq -c will get you your counts for each line