LinuxQuestions.org
Visit Jeremy's Blog.
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 09-24-2010, 12:05 PM   #1
fs11
Member
 
Registered: Aug 2006
Posts: 79

Rep: Reputation: 15
multiple pattern in one line


Hello All,

I am trying to extract lines from a text file that have multiple patterns in it. The format for the file is below:

R.GPLPAAPPAAPERQPS*WER.S
R.GQS*PVSRET*APPVPAARAR.T
R.GQS*PVS*RETAPPVPAARAR.T
R.GQSPVS*RET*APPVPAARAR.T
K.GIPFAT*AKT*LENPQR.H
K.GLHVRAAS*VS*AKGM#SR.K

The output for the files should be the lines that have more than one *.
The problem is that * is also a wild character for sed and grep etc. and I am not able to get the correct expression. Any help would be appreciable.

Thanks in advance.
 
Old 09-24-2010, 12:17 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Put \ before * and other symbols when you use sed and grep etc. Then they behave like regular letters.
 
Old 09-24-2010, 12:28 PM   #3
fs11
Member
 
Registered: Aug 2006
Posts: 79

Original Poster
Rep: Reputation: 15
I am trying to use this:

Quote:
sed '/\*/!d; /\*/!d filename
but it doesnt work
 
Old 09-24-2010, 12:39 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I think your regex is not detailed enough, nor the sed command complete enough, in post #3, to do what you expect it to. Meanwhile, here's an example using grep:

Code:
sasha@reactor: cat asterisk.sh

R.GPLPAAPPAAPERQPS*WER.S
R.GQS*PVSRET*APPVPAARAR.T
R.GQS*PVS*RETAPPVPAARAR.T
R.GQSPVS*RET*APPVPAARAR.T
K.GIPFAT*AKT*LENPQR.H
K.GLHVRAAS*VS*AKGM#SR.K

sasha@reactor: grep -e '\*.*\*' asterisk.sh
R.GQS*PVSRET*APPVPAARAR.T
R.GQS*PVS*RETAPPVPAARAR.T
R.GQSPVS*RET*APPVPAARAR.T
K.GIPFAT*AKT*LENPQR.H
K.GLHVRAAS*VS*AKGM#SR.K
sasha@reactor:
Breakdown the regex: '\*.*\*'

The stuff inside the '' means to match: a literal * character followed by any character(s) followed by another literal * character. So broken into its parts, the regex means:
\* = a single * character (the * is escaped)
.* = any character(s)
\* = and again, another * character (again, escaped with the back-slash).

If you want the output to go to a new file, redirect it with a > character into a file.

Last edited by GrapefruiTgirl; 09-24-2010 at 12:41 PM. Reason: removed some [code] tags. - fixed backwards backslash! Ugh!
 
Old 09-24-2010, 01:28 PM   #5
fs11
Member
 
Registered: Aug 2006
Posts: 79

Original Poster
Rep: Reputation: 15
Thanks alot
 
Old 09-24-2010, 07:24 PM   #6
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ ruby -ne 'print if $_.count("*")>1' file
R.GQS*PVSRET*APPVPAARAR.T
R.GQS*PVS*RETAPPVPAARAR.T
R.GQSPVS*RET*APPVPAARAR.T
K.GIPFAT*AKT*LENPQR.H
K.GLHVRAAS*VS*AKGM#SR.K
 
1 members found this post helpful.
Old 09-24-2010, 07:41 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ kurumi - I don't see too much in the way of ruby solutions normally, until you came along and began posting some. I like it - the more ways we have to do stuff, the better! Thanks.
 
Old 09-24-2010, 11:48 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Just as another alternative:
Code:
awk 'split($0,a,"*") > 2' file
 
Old 09-25-2010, 12:03 AM   #9
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by grail View Post
Just as another alternative:
Code:
awk 'split($0,a,"*") > 2' file
why not let awk do the splitting?
Code:
awk -F"*" 'NF>2' file
 
Old 09-25-2010, 12:35 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Touche
 
  


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
multiple pattern search in a single file line by line saheervc Linux - Newbie 2 09-01-2010 11:45 PM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM

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

All times are GMT -5. The time now is 10:25 PM.

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