Count how many times in every file, strings appeared in a directory.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count how many times in every file, strings appeared in a directory.
# 1  
Old 07-16-2012
Count how many times in every file, strings appeared in a directory.

Hello,

I have some files and i want to count how many times a string is appeared in each file.

Lets say :
Code:
#cat fileA
stringA
sdh
stringB
stringA

Code:
#cat fileB
stringB
stringA 
sdb
stringB
stringB

I need the output to be something like:
HTML Code:
fileA: stringA : 2 stringB : 1
fileB: stringA: 1 stringB: 3

Thanks in advance
# 2  
Old 07-16-2012
This sufficient?
Code:
$ for A in file*; do awk '/string/ {_[$1]++} END{print FILENAME":"; for(a in _){print a, _[a]| "sort"}}' $A; done
fileA:
stringA 2
stringB 1
fileB:
stringA 1
stringB 3

Used GNU awk (gawk). If you are on Solaris, try using nawk.

Last edited by zaxxon; 07-16-2012 at 07:18 AM.. Reason: copy & paste error
# 3  
Old 07-17-2012
Thanks a lot for your response.

Actualy this script prints (if i use it for stringB)
PHP Code:
fileA :
*** 
1
fileB
:
*** 
1
*** 
Where *** the first 3 letter of the line which contains the string and 2 means that the line is duplicated.

Thanks!

---------- Post updated 07-17-12 at 03:00 AM ---------- Previous update was 07-16-12 at 05:30 AM ----------

I made that using a combination of two scripts:
Code:
# cat check_one.sh 
#!/bin/bash
echo "$1"
strA=$(cat $1 |grep  stringA |wc -l)
strB=$(cat $1 |grep  stringB |uniq |wc -l) 
echo "stringA: $strA" 
echo "stringB: $strB"

To check all file just execute check_ALL.sh

Code:
cat check_ALL.sh 
#!/bin/bash
for A in file*;do ./check_one.sh $A; done


Thank a lot for the help zaxxon.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

2. Shell Programming and Scripting

Sed or Awk for lines between two strings multiple times and keep the last one

Hi, I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of “Standard” and “Visual”. I will like to get the lines between “Standard” and “Visual” but I only want to retain only the last one e.g. Standard Some words Some words Some... (4 Replies)
Discussion started by: damanidada
4 Replies

3. UNIX for Dummies Questions & Answers

how to count number of times each word exist in a file

I'm trying to count the number of times each word in the file exist for example if the file has: today I have a lot to write, but I will not go for it. The main thing is that today I am looking for a way to get each word in this file with a word count after it specifying that this word has... (4 Replies)
Discussion started by: shnkool
4 Replies

4. Shell Programming and Scripting

The loop was executed $count times

#!/bin/sh count=0 for i in 2 4 6 do echo "i is $i" count='expr $count + 1' done echo "The loop was executed $count times" with these scripts my output is : i is 2 i is 4 i is 6 The loop was executed expr $count + 1 times What should I do to get the value instead of 'expr... (17 Replies)
Discussion started by: ymwong
17 Replies

5. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

6. Shell Programming and Scripting

Match and count the number of times

ile1 Beckham Ronaldo file2 Beckham Beckham_human Ronaldo Ronaldo_spain Ronaldo Ronaldo_brazil Beckham Beckham_manch Zidane Zidane_Fran Rooney Rooney_Eng Output shud be (1 Reply)
Discussion started by: cdfd123
1 Replies

7. Shell Programming and Scripting

printing strings in one X number of times from another

I have one file of numbers 4 5 2 ... And another file of strings aaaaa bbbbb ccccc ddddd eeeee ffffff ... I'd like to print the stings from each line in reverse order with some decoration the number of times listed in the first file such as: Yeah bbbbb aaaaa Yeah bbbbb aaaaa (5 Replies)
Discussion started by: dcfargo
5 Replies

8. Shell Programming and Scripting

count times for one string

I have a file. I want to count the time for one string appears in this file Example: 56 73 34 79 90 56 34 Expected results 2:56 1:73 2:34 (1 Reply)
Discussion started by: anhtt
1 Replies

9. Linux

To find multiple strings count in a file

I need to find the line count of multiple strings in a particular file. The strings are as follows: bmgcc bmgccftp bsmsftp bulkftp cctuneftp crbtftp crmpos cso gujhr I am doing manual grep for each of the string to find the line count. The command i am using right now is: grep mark... (3 Replies)
Discussion started by: salaathi
3 Replies

10. Shell Programming and Scripting

count file(s) under a specific directory

How do I count number of file in a directory. Here is what I want to do. 1. find total number of file in a directory in variable total 2. ftp all the file 3. ftp_return_code=`grep -c "Transfer complete" $logfile` if then exit 0 else exit -1 fi (6 Replies)
Discussion started by: leemjesse
6 Replies
Login or Register to Ask a Question