Problems with Multiple Pattern String Matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with Multiple Pattern String Matching
# 1  
Old 05-25-2012
Need help with Multiple Pattern String Matching

I am facing a problem and I would be grateful if you can help me Smilie

I have a list of words like

Quote:
the and of ...
And I have a datafile like

the box of
the box of tissues out of
of tissues out of
the book, the
the book, the pen and the
the pen and the

I want to find Patterns of “x.*x” where x are the words mentioned, meaning that each line with start with X and followed/finished by X, such as

The.* the
The.* of
The.* and
Of.* the
.
.

But I need each of them to be saved in a different file so one file for “the.*the” and so on.
I would previously create a acceptfile of the words, such as

the
of
and
.
.
.

and read a normal pattern from an acceptfile using
Code:
Grep -F  -f acceptfile.txt mainfile.txt > outputfile.txt

But the problem here is that I look for a substring and not a single word and GREP acceptfile is not reading my “x.*x” if i type that in my acceptfile
On the other hand, I used to use grep for substring patterns matching
Code:
grep 'The.* of' main.txt > output.txt

but now that I have about 10 words and I want all of them to be compared to each other, I would have around 100 patterns of “x.*x” and I don't want to manually sit and put them in
I am new to unix but I am sure there should be way to do this but not yet sure how. Smilie

Note: I don't necessary need to use GREP but this is just what I am familiar with now but happy to learn new things

I would be amazing if you can help me

Thank you in advance
A-V

---------- Post updated at 09:32 AM ---------- Previous update was at 07:09 AM ----------

or it may be simpler to say i have a array or file called X

Quote:
where X = [the of and ...]
and another one called Y

Quote:
where Y = [the of and ...]
the command looks like this
Code:
grep 'The.* of' A1-5word.txt| sed -n -e "s/.*\(The[^)]*of\).*/\1/p" > the-of-lines.txt

i guess i would need a loop to go through each one of the Xs and each one of the Ys and put them either in separate column or file like

Quote:
the-of pattern result
the-the pattern result and etc
I am really trying my best but dont know what to do Smilie

Last edited by A-V; 05-25-2012 at 09:52 AM..
# 2  
Old 05-25-2012
What is the goal, the reason you are doing this, not how you chose to do it.

It looks like you are still stuck on square zero. So let's go back and detemine your requirements. I'm sure you do not want just to play with grep or ls. You want to do something with the files, or report something in the files.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-25-2012
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

String pattern matching and position

I am not an expert with linux, but following various posts on this forum, I have been trying to write a script to match pattern of charters occurring together in a file. My file has approximately 200 million characters (upper and lower case), with about 50 characters per line. I have merged all... (5 Replies)
Discussion started by: biowizz
5 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. UNIX for Dummies Questions & Answers

Extracting sub-string matching the pattern.

Hi, I have a string looks like the following: USERS 32767.9844 UNDOTBS1 32767.9844 SYSAUX 32767.9844 SYSTEM 32767.9844 EMS 8192 EMS 8192 EMS_INDEXES 4096 EMS_INDEXES 4096 8 rows selected. How do I extract a sub-string to get the expected output as following: EMS 8192 EMS_INDEXES 4096 ... (3 Replies)
Discussion started by: NetBear
3 Replies

4. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

5. Shell Programming and Scripting

SED multiple pattern matching

Hello sorry for the probably simple question - searching about the forums and Internet, I have not found the answer. Could you tell me please how to do a multiple pattern match with SED So it would be SED searching for "PATTERN1" 'or' "PATTERN2" not 'and' if they happen to fall on the same... (4 Replies)
Discussion started by: lostincashe
4 Replies

6. Shell Programming and Scripting

Fetching string after matching pattern from last

I have a file a file having entries are like @ram@sham@sita @krishan@kumar @deep@kumar@hello@sham in this file all line are having different no of pattern-@. need to fetch the substring after the last pattern. like sita kumar sham thanks in advance (3 Replies)
Discussion started by: saluja.deepak
3 Replies

7. Shell Programming and Scripting

Get matching string pattern from a file

Hi, file -> temp.txt cat temp.txt /home/pradeep/123/a_asp.html /home/pradeep/123/a_asp1.html /home/pradeep/435/a_asp2.html /home/pradeep/arun/abc/a_dfr.html /home/pradeep/arun/123/a_kir.html /home/pradeep/123/arun/a_dir.html .... .... .. i need to get a_*.html(bolded strings... (4 Replies)
Discussion started by: pradebban
4 Replies

8. Shell Programming and Scripting

ksh Multiple Pattern Matching Operators

I figured this would be simple, but I am stuck. Variable longpath="/dir1/dir2/dir3/filename.stuff.morestuff.garbage" I want to end up with just "filename.extra.moreextra". So, I want to get rid of the path and .garbage I want to do this with just ksh internals. So, no sed,grep,awk,expr,... (4 Replies)
Discussion started by: Topaz
4 Replies

9. Shell Programming and Scripting

perl basic multiple pattern matching

Hi everyone, and thank you for your help with this. I am VERY new with perl so all of your help is appreciated. I have tried google but as I don't know the proper terms to search for and could be daunting for a newbie scripter... I know this is very easy for most of you! Thanks! I have a... (4 Replies)
Discussion started by: sinusoid
4 Replies

10. Shell Programming and Scripting

pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as Uplink traffic: Downlink traffic: I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in... (7 Replies)
Discussion started by: Yorkie99
7 Replies
Login or Register to Ask a Question