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

One-liner to fix LaTeX quotation marks

by tim_the_monk (Initiate)
on Oct 14, 2012 at 19:29 UTC ( #998968=perlquestion: print w/ replies, xml ) Need Help??
tim_the_monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I would like to replace all double quotation marks (") in a text document which are not directly followed by the a ītick (') or a backtick (`) with ticks.

For example, the text

This is "an example", but "`this not"'.

Should look like this afterwards:

This is 'an example', but "`this not"'.

The background is LaTeX: I have a german document that contains proper german quotation marks (the 'this not' part above) as well as broken ones (the 'an example' part).

Does anybody know a Perl one-liner on the command line for this?

Thanks in advance,

Tim

I tried
cat file.txt | perl -pe 's/\"[^[\'\`]/"`/g'
but something is wrong, the shell wants more input.

Comment on One-liner to fix LaTeX quotation marks
Select or Download Code
Re: One-liner to fix LaTeX quotation marks
by LanX (Canon) on Oct 14, 2012 at 19:40 UTC
    what you are looking for is called "negative look ahead"

    see http://perldoc.perl.org/perlretut.html#Looking-ahead-and-looking-behind

    DB<100> $txt=q( This is "an example", but "`this not"'. ) => " This is \"an example\", but \"`this not\"'. " DB<101> $txt =~ s/"(?![`'])/'/g => 2 DB<102> $txt => " This is 'an example', but \"`this not\"'. "

    but you might get bitten by shell escaping if you try a one-liner

    Cheers Rolf

      Thanks a lot for the info and link.
Re: One-liner to fix LaTeX quotation marks
by aitap (Deacon) on Oct 14, 2012 at 19:48 UTC
    cat file.txt | perl -pe 's/\"[^[\'\`]/"`/g'
    but something is wrong, the shell wants more input.
    This is because you can't really escape anything in single quotes in UNIX shell. You have to write the command like this:
    $ perl -pe "s/\"([^'\`])/'\$1/g" This is "an example", but "`this not"'. This is 'an example', but "`this not"'.
    Sorry if my advice was wrong.
Re: One-liner to fix LaTeX quotation marks
by hippo (Curate) on Oct 14, 2012 at 22:59 UTC

    While others have answered your specific question, I shall instead point out that you will almost never want to do this:

    cat file | command

    as it is a waste of a process (the cat) which gains nothing. Instead you can do this:

    command < file

    thus avoiding what used to be called the UUoCA. Nothing to do with perl, but a valuable concept nonetheless.

Re: One-liner to fix LaTeX quotation marks
by grizzley (Chaplain) on Oct 15, 2012 at 07:19 UTC
    If you want to override existing file you can use -i option: perl -i -pe 'your script' file.txt
Re: One-liner to fix LaTeX quotation marks
by CountZero (Bishop) on Oct 15, 2012 at 07:50 UTC
    Perhaps not directly applicable to your post, but nevertheless very useful if you use LaTeX a lot and have to transform non-LaTeX to properly formatted LaTeX: LaTeX::Encode. It has an "intelligent quotes - iquotes" option in its main latex_encode routine.

    single or double quotes around words will be changed to LaTeX single or double quotes; double quotes around a phrase will be converted to "``" and "''" and single quotes to "`" and "'". This is sometimes called "intelligent quotes"
    So once you have replaced your double quotes by single quotes where necessary, you can run your text through latex_encode to get proper opening and closing quotes.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-09-18 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls