Multi Patterns with Grep "$x .*$ y .*$z " ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Multi Patterns with Grep "$x .*$ y .*$z " ?
# 1  
Old 09-03-2012
Question Multi Patterns with Grep "$x .*$ y .*$z " ?

I have a program which I am trying to improve and I hope you can help me
1 ) My main program took a file and list of words such as [the, and, is, that, there]
2) Created a matrix of the patter of 2 by 2s of instances where one word was followed by the other word.
3) Save each pattern in a file
So having a list like
1 the book is black...
2 the dog and its owner...
3 the box of tissues...
4 of the edge ...

The I would have files such as
The.*is the book is
The .* and the bog and

For this I was using the following lines
Code:
For X (the list of the words)
	For Y ( the list of the words)
		grep -o "$X .* $Y " test01.txt  > data/$X-$Y.txt
	done
done

now I want to create a pattern of Three but I tried adding Z to it but it didn't work and I don't know what I am doing wrong
Code:
For X (the list of the words)
	For Y ( the list of the same words)
		For Z (the list of the same words)
			grep -o "$X .* $Y .*$Z " test01.txt  > data/$X-$Y-$Z.txt
		done
	done
done

so the outcome should have a result like
5 the book and the pen
6 there was the book and then there was a

the.*and.*the the book and the
there.*the.*and there was the book and
the.*and.*there the book and then there

I would really appreciate any help
# 2  
Old 09-03-2012
Can you post a sample input file(test01.txt) and a sample of what the (the list of the words) would actually look like in the code:

Code:
for x (the list of the words)
  for y (the list of the same words)
    for z (the list of the same words)
      grep -o "$X .* $Y .*$Z " test01.txt  > data/$X-$Y-$Z.txt
    done
  done
done

# 3  
Old 09-04-2012
I have no specific file or list of words yet but the concept is to see the pattern of things like words that come together or within small distance. For example “strategic business managements” is more likely than “strategic engineering management”

I am testing now on simple texts. Here I attached some lines from a novel (test 01.txt) and put the list as [the be to of and] as they are more frequent and it will help me to see any pattern. Test02 is windows of 10 starting with the list within a sentence and that’s the input for seeing the pattern
And the final result of all of this is

to do is drive to Una and
the morning last August Bank Holiday and forced me to

hope that does make sense.
I have done the same with two words with the code mentioned above and its file but not sure how y it is not working when I add another FOR

I have solved the Issue and it was a syntax error. the line should be corrected with the right lines and breaks otherwise the whole page will be considered as a line
I am still facing some problems but it is related to the loop and not the GREP so I guess I have to ask it in another thread

Last edited by A-V; 09-04-2012 at 01:04 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

4. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

7. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

8. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies
Login or Register to Ask a Question