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 02-09-2013, 04:58 PM   #1
sunil9009
LQ Newbie
 
Registered: Feb 2013
Posts: 1

Rep: Reputation: Disabled
perl regex


hi guys, i am trying to grab just the fqdn ( with domainname as xyz.com) from the file with the following contents.

Quote:
AAA-BBB-CCC aaa-bbb-ccc-2345.xyz.com aa-dg-hj hira.xyz.com
ddd-eee-fff-4567.xyz.com ddd-eee-fff vffg.xyz
sunil.xyz.com mad.xyz.com
eg: Expected output

Quote:
aaa-bbb-ccc-2345.xyz.com
hira.xyz.com
ddd-eee-fff-4567.xyz.com
sunil.xyz.com
mad.xyz.com
I tried the following code, but it resolves partially.

Quote:
while (<>) {
m/\s*(.*?\.xyz.com)/;
print "$1 \n";
}
Any help would be greatly appreciated
 
Old 02-09-2013, 06:24 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
I don't know about perl but this can be solved with tr and grep.

Code:
cat /somefile | tr ' ' '\n' | grep 'xyz\.com$'

Last edited by sag47; 02-09-2013 at 06:26 PM.
 
Old 02-10-2013, 05:15 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure with perl as you need some way to revisit the same line, but in ruby it would look like:
Code:
ruby -ne '$_.scan(/[^.\s]+\.xyz\.com/).each{|x| puts x}' file
Hopefully this might help lead you to a perl solution

This does show that your original solution obviously won't work from the fact that you would need to print not only $1 on some lines.

Last edited by grail; 02-10-2013 at 05:16 AM.
 
Old 02-11-2013, 07:00 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
http://stackoverflow.com/questions/7...ugh-each-match
 
  


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] 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 to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
need help with regex in perl umbrella2 Programming 5 01-15-2009 09:13 AM
Perl regex $ ShaqDiesel Programming 6 08-18-2006 02:40 PM
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM

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

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