exact string match in a word


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers exact string match in a word
# 8  
Old 12-14-2008
Quote:
Originally Posted by dr_sabz
Smilie As this was the most logical thing to try I had already this, but it gives me a total count of all the "hello" from all of the lines.

Perhaps my first email was not clear (sorry for this). Im looking for the number of occurences of the string "hello" on each line of the file.

So if input file has 2 lines:
hellolaylahellolayla
hellohellohello

Then output should be:
2 ("hello" matched in line 1 twice)
3 ("hello" matched in line 2 a total of 3 times)

A count of 5 is not needed!

Cheers for any help in awk, sed or even perl (but im not very clever with perl yet)

Layla
oh thats what you want.
then its more simple try this
Code:
awk 'total=0;{ total += gsub("hello","")}{print total}' filename

# 9  
Old 12-14-2008
Try this:

Code:
awk -F"hello" '{print NF-1}' file

Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.

Regards
# 10  
Old 12-14-2008
Many thanks to vidyadhar85 and Franklin52. Both methods worked well giving the right output.

Vidyadhar85,
I see you reset the count to 0 when each new line begins. Lovely thankyou.
Smilie
# 11  
Old 12-15-2008
Can you give the sample input file and your desired output file?

For exact string match(no substring) -w option is used with grep. Sorry I could get your requirement.
# 12  
Old 12-15-2008
Hi Siba,

I showed the input and output in a previous post, when I first posted my query. Here it is again:

So if input file has 2 lines:
hellolaylahellolayla
hellohellohello

Then output should be:
2 ("hello" matched in line 1 twice)
3 ("hello" matched in line 2 a total of 3 times)

If you see above, the post above the code works correct. Thank you Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help match the exact string

I just want to match "binutils1_test" only, and print the match line only lyang001@lyang001-OptiPlex-9010:/tmp$ cat file zbinutils1_test bbinutils1_test binutils1_test w-binutils1_test lyang001@lyang001-OptiPlex-9010:/tmp$ cat file |grep -w 'binutils1_test' ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. Shell Programming and Scripting

Get the exact match of the string!

Hi All, I am breaking my head in trying to get a command that will exactly match my given string. I have searched net and found few of the options - grep -F $string file grep -x $string file grep "^${string}$" file awk '/"${string}"/ {print $0}' file strangely nothing seems to... (3 Replies)
Discussion started by: dips_ag
3 Replies

3. UNIX for Dummies Questions & Answers

How to grep the exact string / word ?

Hi All, I have a text / log file which contains strings like meta777, 77, meta, 777. Now I want to write a script which can detect a string 'meta#777' in a text file & number of occurence of 'meta', number of #, number 7, 77, 777. I'm using grep -e '77' filename but no luck. It is returning... (5 Replies)
Discussion started by: adc22
5 Replies

4. Shell Programming and Scripting

Finding exact match string

Hi All, I'm writing unix script, it should find exact matching in search string. Looks simple but when i started i'm stuck to find the exact match character string. The unix script reads the records from DB Table. The table will have values something likes these Feed : A Feed File name :... (3 Replies)
Discussion started by: luckybalaji
3 Replies

5. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

6. Shell Programming and Scripting

How to match a exact word in a variable ????

Hi All, Str="online maintenance" if then perform some action I know the above comparison is wrong ..... Actually I am very new to this solaris.. I want to check for online in a variable and perform some action based on that How can I do it? (6 Replies)
Discussion started by: vijaysachin
6 Replies

7. Shell Programming and Scripting

sed to match only exact string only in all occurences

Dear Friends, Anybody knows how to match exact lines only in multilinear. Input file: apple orange orange apple apple orange Desired output: fruit orange apple fruit i used the command (1 Reply)
Discussion started by: vasanth.vadalur
1 Replies

8. Shell Programming and Scripting

Exact Word Match

I'm trying to find a exact word match but couldn't do it. ABC ABC_NE Searching for ABC_NE tried grep -w </ABC_NE/> grep "^ABC_NE$" but didn't worked , any awk variants would also help. ---------- Post updated at 08:40 AM ---------- Previous update was at 06:48 AM ---------- I... (2 Replies)
Discussion started by: dinjo_jo
2 Replies

9. Shell Programming and Scripting

Searching for exact match in a string ??

Hi I have a string of the form XY_X1998.10.500. I want to check in a script that the middle part is always 10. How to achieve this? e.g the input can be XY_X1998.20.500 OR XY_X1998.50.500 OR XY_X1998.10.500. I have to print Yes everytime the middle value is 10 and NO when the middle... (2 Replies)
Discussion started by: skyineyes
2 Replies

10. Shell Programming and Scripting

How do I search a File for a string exact match

Hi, Can you help please. I have the following comand: if ]; then l_valid_string="Y" fi The problem I am trying to solve is that my l_string = ABC and my file contains ABC ABC_EFG I only want back the value ABC exact match. (3 Replies)
Discussion started by: CAGIRL
3 Replies
Login or Register to Ask a Question