how to remove parentheses from a line in a file - need help!
how to remove parentheses from a line in a file - need help!
am 15.09.2007 19:50:56 von PaulS
Hi,
I have a line of text like this:
m1 (d g s b) en (w=wdnfing l=l as=1e-20 ad=1e-20 ps=1e-20 pd=1e-20)
I am reading-in the file in using $_ and I tried to do the following
(without success):
s/)//g;
s/)//g;
I also tried:
s')''g;
s'(''g;
Why do both of these attempts fail? And, could someone show me the right
way (or one possible way) to get the job done?!
Thanks
P.
Re: how to remove parentheses from a line in a file - need help!
am 15.09.2007 21:48:40 von Paul Lalli
On Sep 15, 1:50 pm, pauls wrote:
> Hi,
> I have a line of text like this:
>
> m1 (d g s b) en (w=wdnfing l=l as=1e-20 ad=1e-20 ps=1e-20 pd=1e-20)
>
> I am reading-in the file in using $_ and I tried to do the following
> (without success):
>
> s/)//g;
> s/)//g;
>
> I also tried:
>
> s')''g;
> s'(''g;
>
> Why do both of these attempts fail?
( and ) are special characters in a regexp. They need to be escaped.
s/\(//g;
s/\)//g;
> And, could someone show me the right way (or one possible way) to
> get the job done?!
In one statement:
s/[()]//g;
or
tr/()//d;
Paul Lalli
Re: how to remove parentheses from a line in a file - need help!
am 15.09.2007 22:54:34 von Dummy
pauls wrote:
> Hi,
> I have a line of text like this:
>
> m1 (d g s b) en (w=wdnfing l=l as=1e-20 ad=1e-20 ps=1e-20 pd=1e-20)
>
>
> I am reading-in the file in using $_ and I tried to do the following
> (without success):
>
> s/)//g;
> s/)//g;
>
> I also tried:
>
> s')''g;
> s'(''g;
>
> Why do both of these attempts fail?
Because the '(' and ')' characters mean something special to a regular expression.
> And, could someone show me the right
> way (or one possible way) to get the job done?!
Don't use regular expressions:
tr/()//d;
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Re: how to remove parentheses from a line in a file - need help!
am 16.09.2007 17:41:58 von PaulS
pauls wrote:
> Hi,
> I have a line of text like this:
>
> m1 (d g s b) en (w=wdnfing l=l as=1e-20 ad=1e-20 ps=1e-20 pd=1e-20)
>
>
> I am reading-in the file in using $_ and I tried to do the following
> (without success):
>
> s/)//g;
> s/)//g;
>
> I also tried:
>
> s')''g;
> s'(''g;
>
> Why do both of these attempts fail? And, could someone show me the right
> way (or one possible way) to get the job done?!
>
> Thanks
>
> P.
Thanks guys, that helped a lot!
What does it mean to "escape" from regxpr ?
This kind of thing comes-up all the time for me as it seems I am
frequently trying to massage a text file to remove things Like
parentheses , brackets and also quotes and dollar signs $.
Thanks!
Paul
Re: how to remove parentheses from a line in a file - need help!
am 16.09.2007 18:48:12 von Bob Walton
pauls wrote:
....
> What does it mean to "escape" from regxpr ?
There are a number of characters that have special meaning in Perl
regular expressions, such as ^.*+?$()[]{}|\ . For the complete list, see:
perldoc perlre
In addition, a regular expression has a delimiter character, / by
default. If you desire these characters to have their literal meaning
rather than their special meaning, they must be "escaped". Usually this
is done with a \ preceding the character. For example:
/a.b/ will match the strings aab, abb, acb, adb, ...
/a\.b/ will match only the string a.b
Note that . is a regular expression metacharacter that has the property
of matching almost any single character when it appears in a regexp.
HTH.
....
> Paul
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
Re: how to remove parentheses from a line in a file - need help!
am 18.09.2007 15:23:44 von chris-usenet
pauls wrote:
> I am reading-in the file in using $_ and I tried to do the following
> (without success):
> s/)//g;
> s/)//g;
This has already been answered by others, but what's puzzling me is that
over here on perl 5.8.8 this triggers an appropriate error message:
$ perl -e 's/(//'
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE / at -e line 1.
Yet pauls doesn't allude to this at all. Is this message a relatively
new feature?
Cheers
Chris
Re: how to remove parentheses from a line in a file - need help!
am 18.09.2007 16:14:46 von Paul Lalli
On Sep 18, 9:23 am, Chris Davies wrote:
> pauls wrote:
> > I am reading-in the file in using $_ and I tried to do the
> > following (without success):
> > s/)//g;
> > s/)//g;
>
> This has already been answered by others, but what's puzzling me
> is that over here on perl 5.8.8 this triggers an appropriate error
> message:
>
> $ perl -e 's/(//'
> Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE / at
> -e line 1.
>
> Yet pauls doesn't allude to this at all.
pauls's descriptions of the error were: "I tried to do the following
(without success):" and "Why do both of these attempts fail?". He
never indicated whether that failure was incorrect results or syntax
errors. I'm 95% willing to bet he got the same syntax error you did,
but didn't think it was worth mentioning.
Paul Lalli