in reply to Inserting a single-quote into a file

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.

Replies are listed 'Best First'.
Re: Inserting a single-quote into a file
by Abigail-II (Bishop) on Jun 19, 2002 at 15:50 UTC
    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