exact string match in a word


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers exact string match in a word
# 1  
Old 12-13-2008
exact string match in a word

Hi all,

Is anyone able to help with the following query?

I have an input file with several lines of words, e.g.
"hellolaylahello"
"hellohellohellolayla"

I want to search for the exact string "hello" in each line and display:
2 "hellolaylahello"
3 "hellohellohellolayla"

I have tried:
echo 'hellolaylahello' | awk '{print match($0, "hello")}' (but I only get the first instance of where hello begins.) If I can get a count OR the start positions it would be a great help.

Cheers
Layla
# 2  
Old 12-13-2008
this will give you the count of hello in a line
Code:
echo 'hellolaylahello' |awk '{ total += gsub("hello","")}END { print total }'

# 3  
Old 12-13-2008
Thank you..I have just spent the past one hour trying all sorts of other ways using R program, but to no avail. Can I combine a loop into the code you provided to do the command to all lines in a file?

I tried the following but I got 0 for each line:
awk -F, '{x=0;for(i=1;i<=NF;i++)if($i == "hello" ) x++; print x}' test
# 4  
Old 12-13-2008
Code:
awk '{ total += gsub("hello","")}END { print total }' filename

just provide the filename which contains hello it will give the total count including all lines
# 5  
Old 12-14-2008
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
# 6  
Old 12-14-2008
use -w option to grep

grep -w "search string" file name
# 7  
Old 12-14-2008
Thank you but this is already a tried method and does not work if you want to search for a string in a continuous word. eg.

filein:
hellohellolayla

grep -w "hello" filein
Returns a blank, because I am searching for a substring and not a whole string as the option -w forces.

Thanks anyway...any other help from anyone...I am close Smilie but need a little more advice
 
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