REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES 
Author Message
 REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES

Hi,

I am trying to look for the string

.uk,|/|end-of-string

this regular expression can be constructed as follows:

'\.uk,|/|$'

Now, suppose I stick the text uk into a variable called $suffix.
I can no longer use single quotes, because I destroy the meaning of
$suffix. If I use double quotes, though, then i Destroy the special
meaning of the metacharacter $.

"\.$suffix,|/|$"

Help!

Never mind,

I wound up using "\.$suffix"',|/|$', but this problem is so persistent
and pervasive in awk, csh, sed, and even perl that I wonder if there
are better-documented solutions.

Also, what is csh's equivalent of the \b,\w,\s constructs in perl?

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 02 Jun 2002 03:00:00 GMT  
 REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES

Quote:

>Hi,

>I am trying to look for the string

>.uk,|/|end-of-string

>this regular expression can be constructed as follows:

>'\.uk,|/|$'

>Now, suppose I stick the text uk into a variable called $suffix.
>I can no longer use single quotes, because I destroy the meaning of
>$suffix. If I use double quotes, though, then i Destroy the special
>meaning of the metacharacter $.

>"\.$suffix,|/|$"

>Help!

>Never mind,

>I wound up using "\.$suffix"',|/|$', but this problem is so persistent
>and pervasive in awk, csh, sed, and even perl that I wonder if there
>are better-documented solutions.

The approach usually used is to "escape" the $ just as you "escaped"
the . at the beginning.  Thus you would write:

"\.$suffix,|/|\$"

This will cause the shell not to treat that $ as a special character.

I believe this is probably mentioned in the man page for the shell.

man sh

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 02 Jun 2002 03:00:00 GMT  
 REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES
Thanks Charles,

However,

I WANTED to retain the special meaning of metacharacter $.

My problem was, in a nutshell,

 I had to construct a pattern match; portions of that pattern match
required single quotes, while others required double quotes.

I wound up using the following:

egrep "\.$suffix"'(,|\/|$)', or equivalently, in perl,

if ( m#(\.$suffix)(,|$|\/)# )

---Siva

Quote:
> >Hi,

> >I am trying to look for the string

> >.uk,|/|end-of-string

> >this regular expression can be constructed as follows:

> >'\.uk,|/|$'

> >Now, suppose I stick the text uk into a variable called $suffix.
> >I can no longer use single quotes, because I destroy the meaning of
> >$suffix. If I use double quotes, though, then i Destroy the special
> >meaning of the metacharacter $.

> >"\.$suffix,|/|$"

> >Help!

> >Never mind,

> >I wound up using "\.$suffix"',|/|$', but this problem is so
persistent
> >and pervasive in awk, csh, sed, and even perl that I wonder if there
> >are better-documented solutions.

> The approach usually used is to "escape" the $ just as you "escaped"
> the . at the beginning.  Thus you would write:

> "\.$suffix,|/|\$"

> This will cause the shell not to treat that $ as a special character.

> I believe this is probably mentioned in the man page for the shell.

> man sh

> Chuck Demas
> Needham, Mass.

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.


Sent via Deja.com http://www.deja.com/
Before you buy.


Tue, 04 Jun 2002 03:00:00 GMT  
 REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES
Hi Charles,

Sorry for the duplication.

Thanks for all of the help....  Let me clarify my earlier post.

You misunderstood. I DIDN'T WANT to destroy the special meaning of the
metacharacter $, which represents end-of-line. Your solution should
work, but, anytime I put a $ in double quotes, the shell seems to
expect either (a) a variable or (b) a literal $, neither of which I
wanted. What I wanted was the $ that is used for anchoring purposes.

--SIva



Quote:


> >Hi,

> >I am trying to look for the string

> >.uk,|/|end-of-string

> >this regular expression can be constructed as follows:

> >'\.uk,|/|$'

> >Now, suppose I stick the text uk into a variable called $suffix.
> >I can no longer use single quotes, because I destroy the meaning of
> >$suffix. If I use double quotes, though, then i Destroy the special
> >meaning of the metacharacter $.

> >"\.$suffix,|/|$"

> >Help!

> >Never mind,

> >I wound up using "\.$suffix"',|/|$', but this problem is so
persistent
> >and pervasive in awk, csh, sed, and even perl that I wonder if there
> >are better-documented solutions.

> The approach usually used is to "escape" the $ just as you "escaped"
> the . at the beginning.  Thus you would write:

> "\.$suffix,|/|\$"

> This will cause the shell not to treat that $ as a special character.

> I believe this is probably mentioned in the man page for the shell.

> man sh

> Chuck Demas
> Needham, Mass.

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.


Sent via Deja.com http://www.deja.com/
Before you buy.


Tue, 04 Jun 2002 03:00:00 GMT  
 REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES

Quote:

>Hi Charles,

>Sorry for the duplication.

>Thanks for all of the help....  Let me clarify my earlier post.

>You misunderstood. I DIDN'T WANT to destroy the special meaning of the
>metacharacter $, which represents end-of-line. Your solution should
>work, but, anytime I put a $ in double quotes, the shell seems to
>expect either (a) a variable or (b) a literal $, neither of which I
>wanted. What I wanted was the $ that is used for anchoring purposes.

Yes, I understood what you were trying to do.

Escaping the $ as I showed protects it from the shell treating it
as a special character, but it doesn't protect it from the command
interpreting it as a special character.

Essentially, there are two steps, shell expansion, then command
interpretation.  

It's important to understand that double quotes allow shell expansion,
and single quotes don't.

FWIW, if you wanted to treat the $ as if it were a character, not
a line anchor you would use something like this:

egrep "\.$suffix,|/|\\\$"
or
egrep "\.$suffix,|/|"'\$'
or
egrep '.'"$suffix"',|/|\$'

Chuck Demas
Needham, Mass.

Quote:
>--SIva





>> >Hi,

>> >I am trying to look for the string

>> >.uk,|/|end-of-string

>> >this regular expression can be constructed as follows:

>> >'\.uk,|/|$'

>> >Now, suppose I stick the text uk into a variable called $suffix.
>> >I can no longer use single quotes, because I destroy the meaning of
>> >$suffix. If I use double quotes, though, then i Destroy the special
>> >meaning of the metacharacter $.

>> >"\.$suffix,|/|$"

>> >Help!

>> >Never mind,

>> >I wound up using "\.$suffix"',|/|$', but this problem is so
>persistent
>> >and pervasive in awk, csh, sed, and even perl that I wonder if there
>> >are better-documented solutions.

>> The approach usually used is to "escape" the $ just as you "escaped"
>> the . at the beginning.  Thus you would write:

>> "\.$suffix,|/|\$"

>> This will cause the shell not to treat that $ as a special character.

>> I believe this is probably mentioned in the man page for the shell.

>> man sh

>> Chuck Demas
>> Needham, Mass.

>> --
>>   Eat Healthy    |   _ _   | Nothing would be done at all,

>>   Die Anyway     |    v    | That no one could find fault with it.

>Sent via Deja.com http://www.deja.com/
>Before you buy.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 04 Jun 2002 03:00:00 GMT  
 REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES

Quote:

> 2. Moshe, you misunderstood. I DIDN'T WANT to destroy the special
> meaning of the metacharacter $, which represents end-of-line. Your
> solution should work, but, anytime I put a $ in double quotes, the
> shell seems to expect either (a) a variable or (b) a literal $,
neither
> of which I wanted. What I wanted was the $ that is used for anchoring
> purposes.

Someone in comp.lang.awk has developed this subject some for you. Go
check out that posting. In short it is about you shouldn't worry about
if the shell interprets the $ litterally, in fact that's what your
want. It's sed and perl that you want to treat it as a line end anchor.

Also. In perl check out the \A anchor, which might or might not help
you here.

Quote:
> 3. Peter, why the injunction to avoid csh scripting? Others have also
> warned me to steer away from csh... Why is this? The reason i like csh
> is that I prefer some of its constructs, like the foreach loop, which
> has no analogue in any of the Bourne-shell variants. I'm also more
> familiar with the command-line syntax ( setting variables and aliases)
> of C-shell variants than those of the Bourne shell variants.

Make sure to check out the link that the other Peter provided. Anyway.
Your reasons for wanting to use the csh are the classical ones. These
were about the same reasons why I, a long time ago, choosed to use the
csh for my serious scripting tasks. Ouch! I regret that to this day.
The problems with the csh are several. The main problem is that is is
full of bugs. It's inconsistent and it _will_ cause you big time
troubles. At first you will start blaming yourself and think you have
written a shitty script. But after a while it will start to sink in
that it's csh causing you problems. Some problems can be fixed by
simple moving a function definition somewhere else in the script!! (At
least that's what happened to me. And that was when I really realized
csh was a bad choice of scripting language.)

Nicole Hamilton, author of the Hamilton CSH, has recently educated me
about the reasons why other csh's behave like this. Look for her
postings in comp.lang.awk and you'll find this particular posting. The
Hamilton CSH should be dealing with all these problems. Problem with
that is that it is only available for NT. Even if you are on NT you
might hesitate since the scripts won't be portable outside the Hamilton
CSH, since all other csh's are buggy as hell. (Nicole told me they are
all based on the same source.)

Note that I still use csh as my interactive shell (or tcsh anyway). I
love it. It's scripting I will never ever again do in it.

And what do you mean with no foreach equivalent? Even the antique sh
has it:

for num in 1 2 3 4 5
do
  echo foo $num
done

Or globbing:

for file in *.foo
do
  echo "Foo file:" $file
done

But use ksh for scripting. It rocks. bash rocks too. Though in many
production machines you won't find bash. You will always find ksh
though. You will always find awk as well. It rocks too for scripting
tasks. It's very c like in it's syntax and it's also very easy to learn.

Sorry for going to such length about this. But csh has hurt me bad when
I was a kid (well, metaphorically speaking).

Regards,
/Peter
--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 05 Jun 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. replace single quote to double quote with an example

2. Getting awk (or sed or anything else) to put single quotes into file

3. Substituting single quotes from Perl via shell using awk and gsub

4. single-quoted string conversion to triple-quoted string

5. single vs. double quotes

6. Single vs double quotes and performance?

7. REXML and double, rather than single quotes

8. Single and Double Quotes in Echo Statements

9. INCLUDE statements: use single or double quotes?

10. Python Tutorial: double single quotes?

11. Difference between double and single quotes

12. single/double quote escape interpolation

 

 
Powered by phpBB® Forum Software