Help with grep & count in ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with grep & count in ksh
# 1  
Old 10-05-2011
Help with grep & count in ksh

Hello,

I have input file with queue names
HTML Code:
  No.esprd.Queue|No.esdev.Queue|||n|120|No_User||No_User|
  No.esdev.Queue|No.esdev.Queue|||n|120|No_User||No_User|
I have to check if the input file contains word "esprd" and the number of times it occurs. I will have to do further processing based on the no of occurances of "esprd". Can you please help me out with this.

Thanks.
# 2  
Old 10-05-2011
depending on how elaborate you want to be....
Code:
grep -c 'esprd' myFile

# 3  
Old 10-05-2011
thanks vgersh99

Quote:
Originally Posted by vgersh99
depending on how elaborate you want to be....
Code:
grep -c 'esprd' myFile

My problem is it does not give me the count of words. It gives me the count of lines. In production region "esprd" should occur only 4 times in the queues. It test region it should occur 0 times. So I want to get the count of words and then process depending on the count.
# 4  
Old 10-05-2011
how 'bout this:
Code:
nawk '{n+=gsub("esprd","")}END{print n}' myFile

# 5  
Old 10-05-2011
Thanks vgersh99. It seems working and i will include this code in my scripts to see if i get required results. Can you please detail me on the code(ofcourse if u have time or else i will figure out myself)
# 6  
Old 10-05-2011
gsub replaces "esrpd" for "" in the line and returns the count of the number of times it did so. This is run for each and every line. n keeps a running total. when it runs out of lines, it prints n.
# 7  
Old 10-05-2011
from 'man awk':
Code:
       sub(r, t, s)
              substitutes t for the first occurrence of the regular expression
              r in the string s.  If s is not given, $0 is used.

       gsub   same as sub except that all occurrences of the  regular  expres-
              sion  are  replaced;  sub and gsub return the number of replace-
              ments.

'n' accumulates the number of replacements and gets printed at the 'END' after all the file(s) have be processed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

2. Shell Programming and Scripting

Count & Result

Hi All. I'm trying to count with command cat Count.log |sort -t 1 | uniq -c Input File. Back-end Invalid Id Password Back-end Invalid Id Password Success Success Success Back-end Invalid Id Password Back-end User state is invalid Back-end User state is invalid Success... (9 Replies)
Discussion started by: ooilinlove
9 Replies

3. 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

4. Shell Programming and Scripting

KSH: Split String into smaller substrings based on count

KSH HP-SOL-Lin Cannot use xAWK I have several strings that are quite long and i want to break them down into smaller substrings. What I have String = "word1 word2 word3 word4 .....wordx" What I want String1="word1 word2" String2="word 3 word4" String3="word4 word5" Stringx="wordx... (5 Replies)
Discussion started by: nitrobass24
5 Replies

5. Shell Programming and Scripting

Count lines and use if then ksh

I try to count number of lines of a data.txt file and then if number of lines is greater than 1 then email me the file. I could not find what is wrong with my code, hope you can point out the mistake i made #! /bin/ksh count =`cat /from/file/data.txt | wc -l` if ]; then mailx -s... (4 Replies)
Discussion started by: sabercats
4 Replies

6. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

7. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

8. Shell Programming and Scripting

combination between || and && in IF condition with ksh

Dear All, Please advice about this issue. when i run this line in a script if && || && || && if i enter $x = test3 and $y = test1 the If condition apply while it should not Best Regards (2 Replies)
Discussion started by: islam.said
2 Replies

9. UNIX for Dummies Questions & Answers

Difference between grep, egrep & grep -i

Hi All, Please i need to know the difference between grep, egrep & grep -i when used to serach through a file. My platform is SunOS 5.9 & i'm using the korn shell. Regards, - divroro12 - (2 Replies)
Discussion started by: divroro12
2 Replies

10. UNIX for Dummies Questions & Answers

Sorting using count, grep and count

Hi, I trying to sort information in a file by making count for every object. For example: A B A D A B C i would like to sort out count for each object (A's, B's and etc) but in actual case i dont know the object but i know the position ofthe object. do i need to use array as well? Any... (2 Replies)
Discussion started by: sukhpal_78
2 Replies
Login or Register to Ask a Question