LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-16-2011, 08:53 AM   #1
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 442

Rep: Reputation: 17
help for regex


hello
I want a regex that match just to :
Quote:
To: 587687685
or
To: +587687685
and not
To: 234mail432@domain3243.com
an I made these but none of them work:
Quote:
^To: \\+?[^A-Za-z]
^To: \\+*[^A-Za-z]
^To: \+*[^A-Za-z] : this one gives me this error in sed: "sed: -e expression #1, char 19: Invalid preceding regular expression"
^To: [^A-Za-z]{10,}
^To: [0-9]{10,}
^To: [0-9]{10,}$
^To: \+?[\d\s]{10,}$
^To: [\d\s]{10,}$
my real story is I want to sed a file that the content between :
Quote:
To: 587687685
or
To: +587687685
and not
To: 234mail432@domain3243.com
and at the end of file :
Quote:
file ended

is needed.
like this command:
Quote:
sed -e '/^To: [\d\s]{10,}$/,/file ended/!d'
thanks.
 
Old 04-16-2011, 09:17 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,578
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
Wouldn't this be enough in your case?
Code:
'/^To:.*[0-9]$/'

Last edited by sycamorex; 04-16-2011 at 09:30 AM.
 
Old 04-16-2011, 09:29 AM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,681

Rep: Reputation: 419Reputation: 419Reputation: 419Reputation: 419Reputation: 419
Heres an input file:

Code:
 cat foo
To: 587687685
or
To: +587687685
and not
To: 234mail432@domain3243.com
Here's a way to just select the 'To:' lines that you want

Code:
 sed -ne '/^To: [+0-9]*$/p' foo
To: 587687685
To: +587687685
From there its pretty easy to add lines or do a range. I'm not sure what you are trying to do with [0-9]{10,} which means to match strings of at least 10 digits. None of your examples have 10 digits so they won't match.

Post back here if you still can't get it to do what you want.
 
Old 04-16-2011, 09:32 AM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,681

Rep: Reputation: 419Reputation: 419Reputation: 419Reputation: 419Reputation: 419
Quote:
Originally Posted by sycamorex View Post
Wouldn't this be enough in your case?
Code:
'/^To:.*[0-9]$/p'
This will match:
To: wrongstuff5

I think golden_boy615 is looking for just digits and '+' in the To line.
 
Old 04-16-2011, 09:33 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
I think the OP was wanting something that would match:

Code:
To: +587687685
Foo
file ended
in the file:

Code:
To: anon@ombramail.com
Bar
To: +587687685
Foo
file ended
Certainly, they seemed to suggest that in their post, and then the code they posted at the end included the text 'file ended', even if it didn't make sense in the command... Though I don't know how to do what they want in sed :S

Last edited by Snark1994; 04-16-2011 at 09:34 AM. Reason: Gender neutrality ;)
 
1 members found this post helpful.
Old 04-16-2011, 09:37 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,578
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
Quote:
Originally Posted by smallpond View Post
This will match:
To: wrongstuff5

I think golden_boy615 is looking for just digits and '+' in the To line.
That's why I asked if that would suffice. Assuming that the Lines starting with To:
Can only end in a digit (ie. a phone number) or an email address (which can't end with a digit) then it would be sufficient.
Obviously, it depends on the the content of the OP's file.
 
Old 04-16-2011, 09:46 AM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,578
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
Quote:
Originally Posted by Snark1994 View Post
Certainly, they seemed to suggest that in their post, and then the code they posted at the end included the text 'file ended', even if it didn't make sense in the command... Though I don't know how to do what they want in sed :S
That would do it:

Code:
sed -n '/pattern/,$p' infile
 
Old 04-16-2011, 10:00 AM   #8
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,681

Rep: Reputation: 419Reputation: 419Reputation: 419Reputation: 419Reputation: 419
Quote:
Originally Posted by sycamorex View Post
That's why I asked if that would suffice. Assuming that the Lines starting with To:
Can only end in a digit (ie. a phone number) or an email address (which can't end with a digit) then it would be sufficient.
Obviously, it depends on the the content of the OP's file.
True. It would help to have a more clear definition of exactly what to match and what to print.
Looking at the attempts, my regexp is also incomplete if the optional '+' has to be before the string of digits. If so, it should be:

sed -ne '/^To: +\?[0-9]*$/p' foo
 
1 members found this post helpful.
Old 04-16-2011, 10:17 AM   #9
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,681

Rep: Reputation: 419Reputation: 419Reputation: 419Reputation: 419Reputation: 419
Quote:
Originally Posted by sycamorex View Post
That would do it:

Code:
sed -n '/pattern/,$p' infile
Yes. I think to print the lines from the matching To: down to 'file ended' is:

Code:
sed -ne '/^To: +\?[0-9]*$/,/file ended/p' foo
 
1 members found this post helpful.
Old 04-17-2011, 01:23 AM   #10
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 442

Original Poster
Rep: Reputation: 17
thanks all
Snark1994 got exactly what I want and smallpond did it:
Quote:
sed -ne '/^To: +\?[0-9]*$/,/file ended/p' foo
thanks to all of you but another question, why these one do not work?
Quote:
sed -ne '/^To: \\+?[0-9]*$/,/file ended/p' foo
or
sed -ne '/^To: \+?[0-9]*$/,/file ended/p' foo
or
sed -ne '/^To: +?[0-9]*$/,/file ended/p' foo
 
Old 04-17-2011, 05:55 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
In sed and grep, the "advanced" regex operators ?+|{}(), are disabled by default and treated literally. You use a backslash escape in front of them to enable their regex meanings.
Code:
/^To: \\+?[0-9]*$/
reads:
"start of line"
"To:"
"a literal space"
"a literal backslash" (the first backslash escapes the second)
"a literal plus sign"
"a literal question mark"
"any number of digits"
"line-end"
Code:
/^To: \+?[0-9]*$/
reads:
"start of line"
"To:"
"one or more spaces" (backslash enables the plus sign so that it means "one or more of the previous character", which is a space)
"a literal question mark"
...etc...
Code:
/^To: +?[0-9]*$/
reads:
"start of line"
"To:"
"a literal space"
"a literal plus sign"
"a literal question mark"
...etc...

Instead of using backslashes for regex, you can use the -r option in sed or the -E option in grep to turn on extended regex expressions. Of course then you have to escape the special characters to turn them off. Your working line could thus also be:
Code:
sed -rne '/^To: \+?[0-9]*$/,/file ended/p' foo
The grep man page explains the difference between basic and extended regex in more detail. The info pages tell you even more.
 
1 members found this post helpful.
Old 04-17-2011, 08:08 AM   #12
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 442

Original Poster
Rep: Reputation: 17
thanks a lot "David the H." finally I found my problem.
 
  


Reply

Tags
regular expressions, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex help TheCrow33 Programming 6 01-09-2011 02:59 PM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 09:08 PM
I need a vi regex d3funct Linux - General 3 03-16-2009 10:26 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 06:10 AM
Regex help Penguin of Wonder Programming 4 08-06-2007 08:04 AM


All times are GMT -5. The time now is 01:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration