LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-20-2012, 03:46 AM   #1
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299

Rep: Reputation: 30
Perl One Liner : Capturing more than one matched regex per line and printing


Something similar to a previous thread of mine :

File = email.txt

Code:
$ cat email.txt
jjjlllll
jksdjklds <<user1@home.com>
<user2@home.com> <user3@home.com>
qwerty
<user4@home.com> 
$ perl -ne ' /<(.*?)>/ && print "$1\n" ' email.txt 
user1@home.com
user2@home.com
user4@home.com
$
What has happened is that a second regex, user3@home.com has not been matched. I have written a short perl script which puts each regex matched into an array and then prints them all but but how could I achieve this using/ modifiying the simple one-liner I have above ? From my limited knowledge I knew above was not going to work as I have used $\1. So how could this be tweaked to capture all the email addresses or will I have to use arrays ?

Thanks.
 
Old 01-20-2012, 05:02 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
perl -ane 'print map {s/<|>//g;"$_\n"} grep {/<(.*)>/} @F' email.txt
-a : split line into @F array
grep field that contains <.*>
map results => remove < and >, add \n

Last edited by Cedrik; 01-20-2012 at 05:51 AM.
 
1 members found this post helpful.
Old 01-20-2012, 06:34 AM   #3
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299

Original Poster
Rep: Reputation: 30
Thanks Cedrik. One thing confuses me here is that why does map come before grep and why cannot you use grep twice ?

Last edited by uncle-c; 01-20-2012 at 06:36 AM.
 
Old 01-20-2012, 06:43 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
map is written before grep but is executed after...

map executes commands for each item in the list
(perldoc -f map)

while grep returns items from list for which the expression evaluated is true
(perldoc -f grep)

Here, the list comes from grep command which extracts fields from @F array that match /<(.*)>/
for each field, map executes:
- s/<|>//g; => remove all < and > from item
- "$_\n" => concatenate item with "\n" char
each item is in $_ variable, map could also be written as:
Code:
map {$_ =~ s/<|>//g; "$_\n"}
(edit)

I found that substitution could be done in grep expression, so no need to use map

Code:
perl -ane 'print grep {s/<+(.*)>/$1\n/} @F' email.txt
- I used ' s/<+(.*)>/$1\n/ ' to match 1 or more '<' like: "<<user1@home.com>"

This substitution could also work...
Code:
perl -ne 'print if s/.*?<+(.*?)>\s?\n?/$1\n/g' email.txt

Last edited by Cedrik; 01-20-2012 at 08:39 AM.
 
1 members found this post helpful.
Old 01-20-2012, 10:03 AM   #5
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299

Original Poster
Rep: Reputation: 30
Thanks Cedrik. I myself was trying to think up the one liner without having to use grep / arrays.
Code:
perl -ne 'print if s/.*?<+(.*?)>\s?\n?/$1\n/g' email.txt
was the the type of code I was thinking along the lines of. Could this be made even simpler ?

Thanks again.
 
Old 01-20-2012, 12:20 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
It is even too simpler (fails with words after email for example)

Hey, look at this reg exp :
http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
 
Old 01-22-2012, 04:16 AM   #7
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299

Original Poster
Rep: Reputation: 30
LOL. The regex to end all regexes !
 
  


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
[SOLVED] Perl one liner - problem with printing captured regex on new line uncle-c Programming 2 01-18-2012 05:15 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl: Multiple line regex matching sammywammy Programming 4 02-09-2011 10:03 AM
Define Perl Regex Logical Line craig467 Programming 3 07-20-2008 10:15 PM
Perl: Running Command line apps in background and capturing output s0l1dsnak3123 Programming 8 03-28-2008 01:24 PM

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

All times are GMT -5. The time now is 03:38 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