LinuxQuestions.org
Register a domain and help support LQ
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 10-15-2009, 10:02 AM   #1
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 314
Blog Entries: 1

Rep: Reputation: 31
Regexp: difference between sed and Perl


I'm trying to use a regexp to remove white spaces where there are more than 2 spaces and leave just one.

I'm testing with sed and this regexp works ok:

Code:
sed -e 's/\s\{2,\}/ /g' teste2.txt
But when I try to use the same regexp in a perl script:

Code:
$line=~s/\s\{2,\}/ /g;
Has no results. It leaves the spaces without removing where there are more than 2 white spaces.

This kind of regexp should be coded different in perl?

Thanks,
Matías
 
Old 10-15-2009, 10:15 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
You don't want to escape the brackets in Perl. By escaping them, you are searching for literally "a space, followed by a bracket, followed by a two, followed by a comma, etc."

Try this:

Code:
$line =~ s/\s{2,}/ /g;
For a friendly tutorial on regular expressions in Perl, start with perldoc perlrequick.

Last edited by Telemachos; 10-15-2009 at 10:18 AM.
 
Old 10-15-2009, 11:03 AM   #3
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 314
Blog Entries: 1

Original Poster
Rep: Reputation: 31
Telemachos,

Thanks a lot for your response. It worked wonderfully!
And thanks for the link too, it'll be usefull.

Regards,
Matías
 
  


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
extract substring using sed and regular expressions (regexp) lindylex Programming 20 12-22-2009 10:41 AM
vim or sed multiline regexp matching eentonig Programming 1 09-08-2008 09:06 AM
SED, regexp or such - remove text after space aolong Linux - General 5 03-07-2008 02:36 PM
help with sed / regexp elinenbe Programming 2 02-01-2008 10:09 AM
perl simple regexp champ Programming 3 07-07-2004 03:27 AM


All times are GMT -5. The time now is 12:35 PM.

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