To Count a particular string with spaces in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To Count a particular string with spaces in a file
# 1  
Old 09-01-2010
To Count a particular string with spaces in a file

I want to count the number of occurence of a particular string with spaces from one text file.
I found one posting of a similar kind but looking for a string without spaces, there were lots of reply's which work when the string does not have any spaces between but in my case i must look into a file for a particular string with spaces.
for example a text file will have data:
Code:
no rows selected no rows selected
ora - 1234 error running sql querry
no rows selected

i should look for "no rows selected"
I the above example i should get a count of 3 since we have "no rows selected" string repeated for three times.

the codes which i found in earlier postings are as below:
Code:
awk '{ for (i=1;i<=NF;i++) if ( $i == "abc") c++ } END{ print c}' sample.txt

and

t
Code:
ypeset -i mCnt=0 
mWord='aaa' 
for mEach in `cat input_file` 
do 
if [ "${mEach}" = "${mWord}" ]; then 
mCnt=${mCnt}+1 
fi 
done 
echo 'Total words for '${mWord}' = '${mCnt}

Above codes work when we do not have spaces between.
Any help is greatly appreciated.

Last edited by vgersh99; 09-01-2010 at 04:29 PM.. Reason: code tags, please!
# 2  
Old 09-01-2010
Code:
perl -0ne '@m=/no rows selected/g;print $#m+1 . "\n"' file

# 3  
Old 09-01-2010
Code:
nawk '{n+=gsub(str,"")}END{print n}' str='no rows selected' mySampleFile.txt

# 4  
Old 09-01-2010
Thank you for the replies.

Sorry i forgot to mention that i have to write my scripts in ksh.

@ vgersh99: when i tried to run your code it throwed me an error as below:
line 1: nawk: command not found

One more thing, my further code depends on the count generated so we need to put this count in a variable which i will be using further.
# 5  
Old 09-01-2010
Quote:
Originally Posted by vpv0002
Thank you for the replies.

Sorry i forgot to mention that i have to write my scripts in ksh.

@ vgersh99: when i tried to run your code it throwed me an error as below:
line 1: nawk: command not found

One more thing, my further code depends on the count generated so we need to put this count in a variable which i will be using further.
If you have Perl installed, then you can run my command in KSH script and assign its output to shell variable...
# 6  
Old 09-01-2010
Quote:
Originally Posted by bartus11
If you have Perl installed, then you can run my command in KSH script and assign its output to shell variable...

I do not have perl installed on my machine.
# 7  
Old 09-01-2010
Code:
#!/bin/ksh

str='no rows selected'

num=$(awk '{n+=gsub(str,"")}END{print n}' str="${str}" mySampleFile.txt)
echo "Total number of [${str}] = ${num}"

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 replace a string in file that is in a certain position with spaces?

I am trying to replace the string in position 26 through 35 of the data file with 10 spaces and I want the remaining file to stay as is, the record length is over 900 characters? I am trying to use the AWK and substr but I am not getting it formatted correctly. Before... (6 Replies)
Discussion started by: fnwine1500
6 Replies

2. Shell Programming and Scripting

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Recursive find / grep within a file / count of a string

Hi All, This is the first time I have posted to this forum so please bear with me. Thanks also advance for any help or guidance. For a project I need to do the following. 1. There are multiple files in multiple locations so I need to find them and the location. So I had planned to use... (9 Replies)
Discussion started by: Charlie6742
9 Replies

4. Post Here to Contact Site Administrators and Moderators

Want a tcl script to compare a string in a file ignoring white spaces

Hi , I want a tcl script to search a string ignoring whitespaces in a .log file . It should correctly match . The string are as follows "Output-Maps 1 1 0 0 0" 1 and Active Intermediate-Maps 0 0 0 ... (1 Reply)
Discussion started by: kulua
1 Replies

5. Shell Programming and Scripting

How to count number of occurances of string in a file?

Gurus, Need little guidance. I have A.txt and B.txt file. B.txt file contains Unique strings. Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file And A.txt file contains the above string as a fourth column which is last column. So A.txt... (7 Replies)
Discussion started by: Shirisha
7 Replies

6. Shell Programming and Scripting

Parse file from remote server to calculate count of string existence in that file

Hi I need to parse the file of same name which exist on different servers and calculate the count of string existed in both files. Say a file abc.log exist on 2 servers. I want to search for string "test" on both files and calculate the total count of search string's existence. For... (6 Replies)
Discussion started by: poweroflinux
6 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

Searching a particular string with spaces in a data file

Hi, I'm new to shell scripting and require your help in achieving the requirement. I have a data file which stores organization name as one of the column data in a csv data file. Organization name stored in data file is: Canadian OU CAD Sample Data file: 1,5,4,5,, ... (9 Replies)
Discussion started by: andy4013
9 Replies

9. UNIX for Dummies Questions & Answers

Grep and count the string in a file.

Hi, I have to grep a word 'XYZ' from 900 files ( from 2007 till date), take its count month wise. The output should display month, count , word 'XYZ' . I tried searching the forum for solution but could find any. I would apprieciate if any one can help me asap .... Many Thanks:) (12 Replies)
Discussion started by: vikram2008
12 Replies

10. UNIX for Dummies Questions & Answers

count string occurance in a file hourly

Hi, I file that has all the status for one day (24hours). Now what I want to do is to count the occurence of a string in its output hourly like for example count occurance of successful or asynchronous clear destinon for every hour and redirect it to file. Please see sample file below. Please... (2 Replies)
Discussion started by: ayhanne
2 Replies
Login or Register to Ask a Question