Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

Replies are listed 'Best First'.
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
    # 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


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 fine.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://175643]
Approved by davis
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-12-17 02:56 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (462 votes). Check out past polls.