Adding values returned through foreach


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding values returned through foreach
# 1  
Old 04-28-2011
Adding values returned through foreach

Hi Folks

I wanted to do something like this :
Go through all the top_level_dirs inside a particular dir.
Grep a particular string in the files in each of those dirs.
Count the occurence of that string in that file
Keep adding these (wc) values so that I can know the total occurences of that string throughout the dirs of that dir.

eg .

I have dir a. and dir b and c are subdirs to a.
Now there is a file 1.log in b/1.log and c/1.log
Code:
pwd ---> a
cat b/1.log | grep 'Hi' | wc -l --> 2
cat c/1.log | grep 'Hi' | wc -l --> 3

I want to grep "Hi" in each of them and display the total occurrences.

Please note, I am trying to do this on Command line and not in a script. Also the dir contents and the log files are astronomical so a "grep -r" is ruled out. Wanted something like,

Code:
Me@My ~]$ foreach d (`ls`)
foreach? echo $d
foreach? grep 'Hi' $d/*.log | wc -l | <Some counter that keeps adding>
foreach? end

Thanks in Advance.
# 2  
Old 04-28-2011
This should do the trick:
Code:
cd a ; find * -maxdepth 1 -name "*.log" -exec grep -c 'string2search4' {} \; | awk '{cnt+=$1}END{print cnt}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: How to avoid warnings for superfluous values returned?

Hi all, a question for the Perl knowledgeable: I have use warnings; enabled. I use something like: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); In the further code I only work with some of the returned variables, as the others I have no need for. Though the... (1 Reply)
Discussion started by: zaxxon
1 Replies

2. UNIX for Dummies Questions & Answers

How to compare to values returned from sql in shell scripting?

hey i am using this code to connect to sql , store the value in variable and then compare it with another variable after some time by executing the same query but the desired result is not coming #!/bin/bash val=$(sqlplus -s rte/rted2@rel76d2 <<ENDOFSQL set heading off set feedback off... (11 Replies)
Discussion started by: ramsavi
11 Replies

3. Shell Programming and Scripting

Adding of two column values

Hi cat /tmp/xx.txt 1 4 1 5 1 6 2 1 2 1 2 1 i want to add the values of 2nd column resepect to 1st column values..for 1 in 1st column i need sum of all the values in 2nd column ..pls tell me hw to do it?? (8 Replies)
Discussion started by: Aditya.Gurgaon
8 Replies

4. UNIX for Dummies Questions & Answers

Adding column with values

Dear all, I need your help for my question please I have without header (space separated) and need to add two colomns at the beginning with values my file look like : rs1 a t 0.6 rs2 a c 0.3 rs3 t g 0.8 I need to a new file like: 1 100 rs1 a t 0.6 1 100 rs2 a c 0.3 1 100 rs3 t g... (3 Replies)
Discussion started by: biopsy
3 Replies

5. Shell Programming and Scripting

Capture query returned values in file.

Hi All, I am connecting to Oracle DB from UNIX script. Want to capture all dates between start date and end date and store them in file. Once this is done, want to read dates one by one. How to achive this in UNIX and Oracle? Please let me know if you have any idea on the same. Thanks and... (4 Replies)
Discussion started by: Nagaraja Akkiva
4 Replies

6. Shell Programming and Scripting

dynamically adding values in c-shell

I am needing to create a variable(changing) and assign it a value(changing) ... I am using C-Shell.. Example: foreach account in ($Accountlist) set account_connect = "$account/$account_pass" end I want to make set account_connect to store various values ? $account_connect did not... (3 Replies)
Discussion started by: shafi2all
3 Replies

7. Shell Programming and Scripting

Adding values concatenating values

I have the following script in a shell # The start of a filename file=$(ls -tr $EMT*.dat | tail -1) # Select the latest file echo $file file_seq=$( < /u02/sct/banner/bandev2/xxxxxx/misc/EFTSQL.dat) echo $file_seq file2 = '$file_seq + 1' echo $file2 It is reading a file EFTSQL.dat... (3 Replies)
Discussion started by: rechever
3 Replies

8. Shell Programming and Scripting

Adding the values of two file

I have two files as Count1 and Count2. The count contains only one values as 10 and count2 contains only one values as 20. Now I want third file Count3 as count1+Count2. That is it should contain sum of two file(10+20=30) (3 Replies)
Discussion started by: Shell_Learner
3 Replies

9. Shell Programming and Scripting

adding values with a loop

Hi there, I am checking disk spaced used on a box # df -k | grep dsk | awk {'print $3'} 2055463 20165785 18310202 32274406 I want to somehow add them up but am no quite sure how to do this in a loop. ...i.e for n in 'df -k | grep dsk | awk {'print $3}' do <some adding... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

10. Shell Programming and Scripting

typeset and values returned from awk output

Somebody can please give a highlight on this. The problem shows only on Linux(Redhat) not any other unix flavors :confused: Linux : $unset m $m=`find . -newer rman_padev_20051206195000.out -name "*L0.rman" -exec ls -l {} \; | awk '{ s+=$5 } END{printf("%.0f", s)}'` $echo $m 7425089536... (0 Replies)
Discussion started by: prathom
0 Replies
Login or Register to Ask a Question