finding the number of occurence of a word in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding the number of occurence of a word in a line
# 1  
Old 06-18-2009
finding the number of occurence of a word in a line

suppose i have this line


abs|der|gt|dftnrk|dtre
i want to count the number of "|" in this line..
how can i do that.
plz helpSmilie
# 2  
Old 06-18-2009
what have you tried?
# 3  
Old 06-18-2009
i tried with grep..
grep -c "|" filename
but it is giving me the count of only the line number in which is pattern is present..i wnt to count the number of occurences of this pattern in a file
# 4  
Old 06-18-2009
Code:
echo "abs|der|gt|dftnrk|dtre" | sed 's/|/
>&/g' |  grep "|" |wc -l


Code:
echo "abs|der|gt|dftnrk|dtre" | awk '{ print gsub("\|","~",$0) }'


Last edited by panyam; 06-18-2009 at 04:20 AM..
# 5  
Old 06-18-2009
You can also use the '-o' flag for grep and then pipe it to wc

Code:
-bash-3.2$ echo "abs|der|gt|dftnrk|dtre" | grep -o "|" | wc -l
4

# 6  
Old 06-18-2009
Code:
 echo "|abs|der|gt|dftnrk|dtre|" | awk -F"|" '{print NF-1}'

# 7  
Old 06-18-2009
MySQL

thanx the last solution is working
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

2. Shell Programming and Scripting

How to find the number of occurence of particular word from a text file?

example: i have the following text file... i am very tired. i am busy i am hungry i have to find the number of occurence of a particular word 'am' from the text file.. can any one give the shell script for it (34 Replies)
Discussion started by: sheela
34 Replies

3. Shell Programming and Scripting

help for fast way of finding line number for a regex

Hello, I am trying to find out the line numbers where regex match and put them into a file with below command: awk '/'$pat'/ {print NR}' $fileName >> temp.txt where $pat is the regex but this command is taking a lot of time to execute with bigger files for size more than 5000000... (8 Replies)
Discussion started by: JoeColeEPL9
8 Replies

4. Shell Programming and Scripting

finding the line number of a particular line in a file

Hi Frnds, I need to find the line number of a particular line in a file and store that line number to a variable. if a file named myfile contains following look at the sun look at the moon look at the star look at the ocean i need to get the line number of the line 'look at the... (3 Replies)
Discussion started by: mvignesh
3 Replies

5. Shell Programming and Scripting

Question on awk for finding the column number using a match word

Hi Guys, Please help me out in my situation of writing a shell script Exampl:I have a output like asnapply 1 2 3 apply_server=1 apply_schema=ASN asnapply 1 2 3 apply_server=2 apply_schema=ASN Now i need output like asnacmd applysever=1 applyschema=ASN stop asnacmd applysever=2... (16 Replies)
Discussion started by: mallak
16 Replies

6. Shell Programming and Scripting

Finding the line with the exact same number

Hello All, What i am doing is , i tail a file from certain chatacter and then cat -n to get the line numbers.I search for a particular string and gets it line number. What i am interested in is the next line immediately after the pattern i search. But grep gives me result for all line... (5 Replies)
Discussion started by: kailash19
5 Replies

7. Shell Programming and Scripting

finding the line number from a grep ?

Hi there does anybody know how i can get the line number from an entry or entries in a file ?? for example if i had a file test1 test2 test3 test1 and i needed to get the line numbers for all instances of test1 in that file with the answer being (1,4) Would anybody be able... (7 Replies)
Discussion started by: hcclnoodles
7 Replies

8. Shell Programming and Scripting

Finding the line number of matching braces

Hi,I am new to shell scripting and i want to find the line numbers of matching braces. The file contents are as follows File XXX.dat 1 ( CLASS "FRUIT" 2 (TYPE "PERSISTENT") 3 (MESSAGE_TYPE "M") 4 (GET_REQRD "Y") 5 (SET_REQRD "Y") 6 ) 7 ( CLASS... (3 Replies)
Discussion started by: Rajendra_1510
3 Replies

9. Shell Programming and Scripting

first occurence and line number

Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... .. total rejected rows: 40 total rejected rows: 50 total rejected rows: 80 total rejected rows: 90 total... (9 Replies)
Discussion started by: mercuryshipzz
9 Replies

10. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies
Login or Register to Ask a Question