Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-14-2009, 08:10 AM   #1
Registered: Nov 2008
Posts: 34

Rep: Reputation: 15
need help with regex in perl

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

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

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Or if you don't want an extra space at the end of each line:
perl -e 'while(<>){$_ =~ s/[(]/ /g;s/[)]//g;print $_;}'
Old 01-14-2009, 10:12 AM   #4
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?
perl -ple 'tr/(/ /; tr/)//d' filename
hektor ~ $ perl -ple 'tr/(/ /; tr/)//d' numbers 80 25 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 10:24 AM.
Old 01-15-2009, 09:09 AM   #5
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...
@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?
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, 10:13 AM   #6
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.
#!/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 11:19 AM.


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 10:51 AM
Capitalization Perl Regex ShaqDiesel Programming 4 10-28-2006 10:38 AM
Perl regex $ ShaqDiesel Programming 6 08-18-2006 03:40 PM
regex Perl help igotlongestname Programming 2 09-14-2005 08:51 PM
perl regex matching exodist Programming 2 11-15-2004 11:50 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:49 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration