Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Inserting a single-quote into a file

by chuntoon (Sexton)
on Jun 19, 2002 at 12:48 UTC ( #175643=perlquestion: print w/ replies, xml ) Need Help??
chuntoon has asked for the wisdom of the Perl Monks concerning the following question:

This may seem like a ridiculous question, but I can't figure it out to save my bacon.

I have a SQL file that is missing a single quote around a text field.

I am attempt to do a global search-and-replace from the command line as this is a one time deal. Unfortunately, it isn't working like I think it should.

When I attempt the following from the unix prompt I get a prompt for more input as follows:

thug:/tmp> perl -p -i -e 's/ where/' where/g' *.sql >
I even attempt to escape the single-quote, to no avail:
thug:/tmp> perl -p -i -e 's/ where/\' where/g' *.sql >
I know it must be something small I am overlooking.

Thanks, chuntoon

Comment on Inserting a single-quote into a file
Select or Download Code
Re: Inserting a single-quote into a file
by Chmrr (Vicar) on Jun 19, 2002 at 12:54 UTC

    Yup, it's a shell quoting problem. Switch up the quotes:

    perl -pi -e"s/ where/' where/g" *.sql

    ..works fine.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Inserting a single-quote into a file
by hacker (Priest) on Jun 19, 2002 at 14:09 UTC
    It's important to know why as well.

    In bash, the standard current Linux shell, your single quotes have to balance out. In your example above, you have three single quotes. Four of them would balance out, but not three. You can see this if you change your code:

    perl -p -i -e 's/ where/' where/g' *.sql
    to:
    # Note the second single-quote below perl -pi.orig -e 's/ where/'' where/g' *.sql
    This probably will not give you the results you expect (though it's "shell-correct"), because the quoting is actually still incorrect for your target results.

    There is also an interesting change I put in the second example. Note the '-pi.orig' change there. What that does is create a file called 'foo.sql.orig' for every file changed by your substitution (assuming foo.sql was one of your files). When you're dealing with files and substitutions "in-place" like that, it's good to make a backup.

    NOTE: If you run this a second time, your backup file will be overwritten by your second copy, trashing your backup. It's always good to have backups somewhere else, but if you're doing a one-time pass, using -pi.orig is a good way to do that.

    The suggestion from Chmrr is correct in this case, you need to change how your quoting is passed to the shell. You do not have to do this if this change was inside a perl script running under bash, however.

    There are several documents on the web that can help you understand this a bit more:

    A Guide to Unix Shell Quoting

    AWK shell quoting issues

    Hope that helps.

      perl -pi.orig -e 's/ where/'' where/g' *.sql

      That's just plain stupid. It's a inconvenient way of writing

      perl -pi.orig -e 's/ where/ where/g' *.sql
      and that's clearly not what the poster intended. One could of course in this case switch to double quotes as the shell quotes, but that's not a general solution. The following is:
      perl -pi.orig -e 's/ where/'"'"'where/g' *.sql
      and this works too:
      perl -pi.orig -e 's/ where/'\''where/g' *.sql

      Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://175643]
Approved by davis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-08-21 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls