LinuxQuestions.org
Help answer threads with 0 replies.
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-18-2012, 06:02 AM   #1
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Lubuntu 14.04 LTS, Kali
Posts: 297

Rep: Reputation: 30
Perl one liner - problem with printing captured regex on new line


I have a txt file , email.txt, full of data in the following format and I basically want to extract all the email addresses.
Code:
jjlkgfjlk
dfkf;k;fkf
<user1@email.com>
kkdsks
ffp;fd;d
<user2@email.com>
dsk;k;kl;dkd
The one-liner I use is :
Code:
uncle-c@home:~/Perl/$ perl -ne '/^<(.*)>/ && print $1 ' email.txt 
user1@email.comuser2@email.comuncle-c@home:~/Perl/$
What looks obvious here is that output of each regex capture needs to be put on a new line for clarity. I've tried alterations along the lines of print $1\n etc but I'm not getting anywhere. Prob something very simple and obvious so apologies for my ignorance :-)
What would really help would be what I am doing wrong and and an explanation of the solution. I am just starting off learning Perl ( self taught) so any input is very very useful.

Thanks.

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

Rep: Reputation: 243Reputation: 243Reputation: 243
You have to add double quotes, so the "\n" char is interpreted

Code:
perl -ne '/^<(.*)>/ && print "$1\n"' email.txt
 
1 members found this post helpful.
Old 01-18-2012, 06:15 AM   #3
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Lubuntu 14.04 LTS, Kali
Posts: 297

Original Poster
Rep: Reputation: 30
Thanks Cedrik ! I feel like complete dodo now but at least I will not forget this elementary mistake in the future.

Last edited by uncle-c; 01-18-2012 at 06:17 AM.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - printing progress on same line. Technoslave Programming 7 09-03-2011 10:20 PM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
Perl: Multiple line regex matching sammywammy Programming 4 02-09-2011 11:03 AM
Define Perl Regex Logical Line craig467 Programming 3 07-20-2008 11:15 PM
PERL editing single line in file with regex indiescene Programming 0 04-14-2004 09:18 AM

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

All times are GMT -5. The time now is 11:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration