LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-14-2009, 07:10 AM   #1
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Rep: Reputation: 15
need help with regex in perl


hi all,
So I've something file, for example...
Code:
10.10.10.10(80)
10.10.10.11(25)
10.10.10.12(53)
..etc
Which's way I can took its?
Code:
10.10.10.10 80
10.10.10.11 25
10.10.10.12 53
..etc
Just without the brackets?
 
Old 01-14-2009, 08:00 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Code:
perl -e 'while(<>){$_ =~ s/[()]/ /g;print $_;}'
--- rod.
 
Old 01-14-2009, 08:16 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Or if you don't want an extra space at the end of each line:
Code:
perl -e 'while(<>){$_ =~ s/[(]/ /g;s/[)]//g;print $_;}'
 
Old 01-14-2009, 09:12 AM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
I don't think that you need a regular expression here. Won't tr do fine?
Code:
perl -ple 'tr/(/ /; tr/)//d' filename
Output:
Code:
hektor ~ $ perl -ple 'tr/(/ /; tr/)//d' numbers
10.10.10.10 80 
10.10.10.11 25 
10.10.10.12 53
We can also use the -p flag to save typing the while (<>) control structure and the print calls explicitly.

Last edited by Telemachos; 01-14-2009 at 09:24 AM.
 
Old 01-15-2009, 08:09 AM   #5
umbrella2
Member
 
Registered: Nov 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Ok, thanks, first sample is suitable.
I've few questions...
1) On that time suggest me please which's way I can took unly file name. For example...
I tried this...
Code:
$fullpath="/usr/local/bin/somedaemon
@a = ~s/.*\///;
print @a"\n";
Just I need have got only file name.
--------------------------------------
2) Which's way usage sort perl function or anything else so as to doing whatever similar 'sort -u' unix-like command?
Quote:
`-u'
For the default case or the `-m' option, only output the first of
a sequence of lines that compare equal. For the `-c' option,
check that no pair of consecutive lines compares equal.
____
Thanks in advance.
 
Old 01-15-2009, 09:13 AM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
You should really post separate threads for new questions, since people often stop checking older threads when they seem to have been answered. You also probably want to spend some time with Perl's internal documentation (check perldoc perldoc) and or a good introductory book on Perl. I like Beginning Perl which is available free online.

You can get take apart a file name using the File::Basename module. It's part of Perl's core, so you don't need to install anything extra.
Code:
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;

my $fullpath = '/usr/local/bin/somedaemon';

my $basename = basename $fullpath;
my $dirname = dirname $fullpath;

print "The full path is $fullpath\n";
print "\tBasename: $basename\n";
print "\tDirname:  $dirname\n";
For getting unique elements from a list, you probably want to use a hash or a module. See the perlfaq answer at perldoc -q duplicate for a few suggestions.

Last edited by Telemachos; 01-15-2009 at 10:19 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
using perl regex in substr lrt Programming 4 12-08-2007 09:51 AM
Capitalization Perl Regex ShaqDiesel Programming 4 10-28-2006 09:38 AM
Perl regex $ ShaqDiesel Programming 6 08-18-2006 02:40 PM
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM


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