Share your knowledge at the LQ Wiki.
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 10-15-2009, 10:02 AM   #1
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 321
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:

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

$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?

Old 10-15-2009, 10:15 AM   #2
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:

$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
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 321
Blog Entries: 1

Original Poster
Rep: Reputation: 31

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



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 > Forums > Non-*NIX Forums > Programming

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