Regex for word followed by another word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex for word followed by another word
# 1  
Old 03-06-2012
Regex for word followed by another word

I want to find a regex command that I can run on the command line that will find a whole word followed by another whole word (that I specify in the command).

What I am looking to do is also include a file extension (like .txt) in the command such that it only runs the regex on files with that extension recursively in a directory structure.

I know how to use the $find command with -exec but have not been able to assemble the regex.
Code:
$find . -name '*.txt' -exec <regex command> {} \;


Last edited by Franklin52; 03-07-2012 at 07:40 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-06-2012
Something like this?

Code:
find ./ -name "*.txt" -exec grep 'word1 word2' {} \;


Last edited by balajesuri; 03-06-2012 at 12:57 PM..
# 3  
Old 03-06-2012
almost

This almost gets me there, tried that, but it does not give me word boundaries.
# 4  
Old 03-06-2012
Give an example of a string that's supposed to match but doesn't, please.
# 5  
Old 03-06-2012
example words

Here are two random words that I just thought up.

the word "find" and the word "me".

So I want to search all .txt files recursively that have the whole word "me" followed by the word "find".

so "find mellow yellow" would fail the regex as does any other word ending in "find".

so I need "<space>find<one or more spaces or any characters>me<space>"

thanks for your help.
# 6  
Old 03-06-2012
Why wouldn't "find me" match "find mellow yellow"? It should. I'm guessing you've given the exact opposite -- a string that shouldn't match, but does.

You've broken down what you want almost exactly though, so:

Code:
egrep "(^| )find( | .* )me( |$)"

This should insist on 'find' and 'me' being whole words, and find them even when they're on the beginning or end of lines. It must find them both on the same line though.
# 7  
Old 03-07-2012
There are differences in Regex engines. Corona used egrep, so you have to use egrep as well.

Finding exact words separated by arbitrary numbers of whitespace can be done like this:
Code:
$ export word1=find
$ export word2=me
$ regex2wd2=$(printf "'%s[   ]+%s'" $word1 $word2)
$ echo "find   me" | egrep "$regex2d"
find   me

the [ ] thing has one space character and one tab character inside the [ ] characters.
Note the single quote characters embedded in the printf format string. They prevents the shell in this case bash, from messing up the regex by trying to interpret goofy characters when you use the string as a regex.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

3. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

4. Shell Programming and Scripting

Regex to identify word in second position on a line

I am interested in finding a regex to find a word in second position on a line. The word in question is या I tried the following PERL EXPRESSION but it did not work: ] या or ^\W या But both gave Null results I am giving below a Sample file: देना या सौंपना=delegate तह जमना या... (8 Replies)
Discussion started by: gimley
8 Replies

5. Shell Programming and Scripting

What is the most widely used word anchor used in Regex?

Hello, All I learned from book about word anchor "\<" and "\>"; however when I tested them, they seem to work only in grep. Can anyone suggest word anchor that can be used in grep, awk, perl ...? (3 Replies)
Discussion started by: littlewenwen
3 Replies

6. Shell Programming and Scripting

Regex: Get the word before match

Hi Input: MYTEXT.aa.bb cc.MYTEXT.aa.bb ee.dd.cc.MYTEXT.aa.bb cc.NOTEXT.a.b Output: <empty> cc cc <empty> I would like to use a regex to extract the last word before MYTEXT without the dot (2 Replies)
Discussion started by: chitech
2 Replies

7. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

8. Shell Programming and Scripting

regex - start with a word but ignore that word

Hi Guys. I guess I have a very basic query but stuck with it :( I have a file in which I want to extract particular content. The content is between standard format like : Verify stats A=0 B=12 C=34 TEST Failed Now I want to extract data between "Verify stats" & "TEST Failed" but do... (6 Replies)
Discussion started by: ratneshnagori
6 Replies

9. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question