LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2008, 10:53 AM   #1
raghu123
Member
 
Registered: May 2008
Posts: 34

Rep: Reputation: 15
Smile Regular expression problem


Hi.........

I hav a file having 3 colums lik this.....

1N1Y.pdb_SIA_lig_1 1V0F.pdb_SLB_lig_1 0.217803
1N1Y.pdb_SIA_lig_1 1V0F.pdb_SLB_lig_3 0.159091
1N1Y.pdb_SIA_lig_1 1V0F.pdb_SLB_lig_6 0.157197
1A4G.pdb_NAG_lig_1 2ZG1.pdb_SIA_lig_1 0.076190
1A4G.pdb_NAG_lig_2 2ZG1.pdb_SIA_lig_1 0.057143
1A4G.pdb_CA_lig_5 2ZG1.pdb_SIA_lig_1 0.000000
1N1Y.pdb_SIA_lig_1 1V0F.pdb_SLB_lig_9 0.092803
1N1Y.pdb_SIA_lig_1 1V0F.pdb_SLB_lig_2 0.092803
1N1Y.pdb_SIA_lig_1 1V0F.pdb_SIA_lig_5 0.081439
1A4G.pdb_ZMR_lig_7 2ZG1.pdb_SIA_lig_1 0.044326
1A4G.pdb_ZMR_lig_6 2ZG1.pdb_SIA_lig_1 0.042553
1A4G.pdb_ZMR_lig_6 2F0Z.pdb_ZMR_lig_1 0.394504
1A4G.pdb_ZMR_lig_7 2F0Z.pdb_ZMR_lig_1 0.391844

I want to grep the patterns in such a way that i get

either SIA/ZMR (middle terms of the 1st and 2nd column) in the 1st column and either SIA/ZMR again in the 2nd column and the 3rd column will be score as usual......

the O/P i explained must b lik this.....

1N1Y.pdb_SIA_lig_1 1V0F.pdb_SIA_lig_5 0.081439
1A4G.pdb_ZMR_lig_7 2ZG1.pdb_SIA_lig_1 0.044326
1A4G.pdb_ZMR_lig_6 2ZG1.pdb_SIA_lig_1 0.042553
1A4G.pdb_ZMR_lig_6 2F0Z.pdb_ZMR_lig_1 0.394504
1A4G.pdb_ZMR_lig_7 2F0Z.pdb_ZMR_lig_1 0.391844


SO print those in which the 1stcolumn has either SIA or ZMR and the 2nd column also havin the corresponding SIA or ZMR

PLz help.......need it badly
 
Old 10-10-2008, 10:55 AM   #2
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
[actually ter are spaces in between the columns wic is not clear wen i paste in the forum.....
the columns are like this......



1A4G.pdb_ZMR_lig_6 2F0Z.pdb_ZMR_lig_1 0.394504
1A4G.pdb_ZMR_lig_7 2F0Z.pdb_ZMR_lig_1 0.391844

jus for sample i typed these 2 entries
 
Old 10-10-2008, 12:57 PM   #3
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Very simple.

Code:
cat <file> | egrep "ZMR.*(ZMR|SIA)|SIA.*(ZMR|SIA)"
[edit] Oops, I misread your post, I though you wanted only lines that contained either tag in both columns. Fixed.

Last edited by raconteur; 10-10-2008 at 01:16 PM.
 
Old 10-10-2008, 02:50 PM   #4
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
plz understand the question...i hav mentioned the correct addressing of my question
 
Old 10-10-2008, 03:02 PM   #5
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by raghu123 View Post
plz understand the question...i hav mentioned the correct addressing of my question
And I gave you a correct answer.
 
Old 10-10-2008, 07:32 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@raconteur
Actually, UUOC (Useless Use Of cat), you can do

egrep "ZMR.*(ZMR|SIA)|SIA.*(ZMR|SIA)" <filename>



PS Like the regex though...
 
Old 10-11-2008, 03:25 AM   #7
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Smile

Thanq..........

the code is working fine
 
Old 10-11-2008, 08:40 AM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by chrism01 View Post
@raconteur
Actually, UUOC (Useless Use Of cat), you can do

egrep "ZMR.*(ZMR|SIA)|SIA.*(ZMR|SIA)" <filename>
...
Actually it's a NACUUOC (Not A Completely Useless Use Of cat) -- cat'ing a file into a short command being iteratively developed on the CLI makes the heart of it, in this case the regex, more accessible for editing. I do it all the time, although I do usually remove the cat later.

I don't understand the "ZMR.*(ZMR|SIA)|SIA" part of the regex. Wouldn't:
Code:
egrep '(ZMR|SIA).*(ZMR|SIA)'
## (untested)
work just as well? Or maybe better:
Code:
egrep '^[^ ]*(ZMR|SIA)[^ ]* [^ ]*(ZMR|SIA)'
## (untested)
 
Old 10-11-2008, 10:14 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
TMTOWTDI


A final soln should probably not use it unless you need to. Shorter lines are easier to read / debug (unless its really obscure).
I can see this degenerating into style discussion

Actually, it can make a difference sometimes (using '|') eg you can echo a string through the wc cmd and get num of words, but you can't use the wc cmd on a string eg

you can

string="one two three"

echo $line|wc -w

but you can't

wc -w $string

in the latter case wc insists on a filename as the input.

I think I'll stop there before I get into trouble
 
Old 10-12-2008, 06:19 AM   #10
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by archtoad6 View Post
Actually it's a NACUUOC (Not A Completely Useless Use Of cat) -- cat'ing a file into a short command being iteratively developed on the CLI makes the heart of it, in this case the regex, more accessible for editing. I do it all the time, although I do usually remove the cat later.
But there's a better solution for that. Put the filename first using redirection:
Code:
<filename egrep "ZMR.*(ZMR|SIA)|SIA.*(ZMR|SIA)"

Last edited by Telemachos; 10-12-2008 at 06:29 AM.
 
Old 10-12-2008, 07:13 AM   #11
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Good point, thanks.

I wonder if that "word order" doesn't spring to my mind because of having English as a native language. Would a speaker of Latin, used to having word order make almost difference to the meaning of a sentence, be more likely to think of that?

Anyway, I must remember that.
 
Old 10-12-2008, 07:17 AM   #12
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by archtoad6 View Post
Would a speaker of Latin, used to having word order make almost difference to the meaning of a sentence, be more likely to think of that?
A teacher of Latin in this case, so maybe you're right.

Last edited by Telemachos; 10-12-2008 at 07:33 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl regular expression problem true_atlantis Programming 4 05-27-2009 06:35 AM
PHP PRCE Regular Expression problem x_terminat_or_3 Programming 4 09-11-2007 04:09 PM
having problem in writing regular expression in tcl mohtasham1983 Programming 1 10-29-2006 01:29 PM
Regular expression datbenik Programming 1 01-05-2006 01:58 PM
OpenOffice regular expression filtering problem JAB4ever Linux - Software 3 12-08-2004 04:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration