LinuxQuestions.org
Review your favorite Linux distribution.
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 05-12-2010, 03:30 PM   #1
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Rep: Reputation: 16
Regular Expression in Perl


I am trying to create a regular expression in perl to remove all the delimiters (|) from a file. I was also thinking might I be able to use tr?

Tarken
 
Old 05-12-2010, 03:40 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Feel free.

Did you have a question? What have you got already?
 
Old 05-12-2010, 04:28 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Code:
sed 's/|//g'
 
Old 05-12-2010, 04:41 PM   #4
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Code:
system(`tr -d "|" <temp.1 > temp.2 &`);
I figured it out. This works fine. Any reason I should not be sending the commands out to the shell?

Tarken
 
Old 05-12-2010, 04:57 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tarken View Post
Code:
system(`tr -d "|" <temp.1 > temp.2 &`);
I figured it out. This works fine. Any reason I should not be sending the commands out to the shell?

Tarken
And Perl has nothing to do with this - you do all the job in shell using 'tr'. And you do not need backtticks as well.
 
Old 05-12-2010, 05:20 PM   #6
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Sergei Steshenko View Post
And Perl has nothing to do with this - you do all the job in shell using 'tr'. And you do not need backtticks as well.
The reason I am writing this in Perl because it will be part of other existing code that is written in Perl.
 
Old 05-12-2010, 05:28 PM   #7
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Rep: Reputation: 15
you might use a shell wrapper, more fancy. And do all the shell processing you need, and then going back to perl with the final data.
Well if you only need to change |, why not:
$string=~ s/\|//g;
Assuming $string contains a text line of the file
 
Old 05-12-2010, 06:16 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tarken View Post
The reason I am writing this in Perl because it will be part of other existing code that is written in Perl.
Which doesn't disprove what I've said - your solution has nothing to do with Perl. A solution in Perl would use Perl regular expressions or Perl built-in 'tr' or 'y' function.
 
Old 05-12-2010, 07:11 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by patolfo View Post
$string=~ s/\|//g;
Assuming $string contains a text line of the file
That's what I was thinking and wanted to suggest — I just forgot the exact Perl s/// syntax.
 
Old 05-12-2010, 07:38 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by MTK358 View Post
That's what I was thinking and wanted to suggest — I just forgot the exact Perl s/// syntax.
Its basically =~ <sed syntax here>.
 
Old 05-12-2010, 07:58 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
sed can do far more than s///.
 
Old 05-12-2010, 08:10 PM   #12
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
^ but thats what its most used for.
On a second note, so can perl.
 
  


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
perl regular expression problem true_atlantis Programming 4 05-27-2009 06:35 AM
regular expression with perl MissEileen Programming 3 03-11-2008 01:26 PM
Perl Regular Expression abdul_zu Linux - Software 2 08-20-2007 03:57 AM
Having trouble with a perl regular expression... jayemef Programming 3 08-25-2005 11:00 PM
Perl Regular Expression rch Programming 14 07-11-2003 11:00 PM

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

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