in reply to Simple Substitution

There's several options for your regex.

Also, instead of -ne, I'd use -pe and get rid of the print statement. I'd also look at making it -pi.bak -e to do in-place editing with a backup file.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^2: Simple Substitution
by winter67uk (Initiate) on Jan 14, 2005 at 10:42 UTC
    Dragonchild (and all who followed) - thank you. This line did the trick for me:

    perl -pe "s/([^?])'/$1'/g" input.txt

    Things you folks taught me:

  • grouping in a regex - see perlrequick
  • the special variable $1 - ditto
  • the -p switch
  • the -i switch
  • check code before posting. I had two errors in mine (extra double quote and unnecessary escape)

    Cheers - Winter

Re^2: Simple Substitution
by winter67uk (Initiate) on Jan 18, 2005 at 11:22 UTC
    Final Update: this is what I used in the end...

    Parses a string in a file. The string uses an apostrophe as line terminator. Ignores apostrophes predeeded by the escape character, "?". Clever enough not to parse the same file more than once.

    perl -p -i.bak -e "s/([^?])'([^\n])/$1'\n$2/g" input.txt

  • perl: Invokes the command interpreter.
  • -p: p switch is 'assume loop like -n but print line also'.
  • -i.bak: i switch is edit in place, .bak is the extension of the backup file.
  • -e: e switch is 'one line of program'.
  • "...": Use double quotes for Windows OS.
  • s/.../.../g: Substitute. Match 1st and substitute 2nd. g means global.
  • (...)...(...): Groups - whatever the value between brackets winds up in $1, $2 etc.
  • ([^?])'([^\n]): Match any apostrophes neither preceeded by the escape character "?" nor followed by a new line "\n" - three characters total.
  • $1'\n$2: Replace matches with the value of group one (see above) followed by an apostrophe and a new line(\n) and the value of group two - four characters total.
  • input.txt: File to parse.