Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i am having a statement like this source "../version.cmd" in a file called as output.cmd . I want that above statement is replaced by this statement source ../version.cmd . i had tried with this command in script perl -pi -e 's/source \"../source ../g' output.cmd but it removes only the " which occur first and the resuting string becomes source ../version.cmd" . i want to remove last " also using same script.
  • Comment on remove a specific character occuring in file using script

Replies are listed 'Best First'.
Re: remove a specific character occuring in file using script
by jbrugger (Parson) on Feb 10, 2005 at 07:56 UTC
    you want to change "../foo" to ../foo?
    see example:
    blah.txt:
    "../foo.blah" rara wewe wewe rara wewe wewe rara "../bar.blah"
    then perform:
    perl -pi -e 's/\"(\.\.\/.*?)\"/$1/g' blah.txt
    update only foo.blah? use:
    perl -pi -e 's/\"(\.\.\/foo\.blah)\"/$1/g' blah.txt

    that results in:
    ../foo.blah rara wewe wewe rara wewe wewe rara "../bar.blah"
    Update 2 ah, if you'd use code tags, it would have been clear to me...
    perl -pi -e 's/(source.*?)\"(\.\.\/.*)\"/$1$2/g' blah.txt
      the solution you had suggested works fine , but problem is that i do not want to remove all occurences of '' in the file. i had told you i want the specific statement source "../xxx.cmd" to be replaces as source ../xxx.cmd. i want only this type of statements not all .
Re: remove a specific character occuring in file using script
by sh1tn (Priest) on Feb 10, 2005 at 08:15 UTC
    UNIX cli:
    perl -i.org -pe 's|"(\.\./version.cmd)"|$1|' filename
    win32 cli:
    perl -i.org -pe "s|\"(\.\./version.cmd)\"|$1|" filename
Re: remove a specific character occuring in file using script
by eyepopslikeamosquito (Archbishop) on Feb 10, 2005 at 08:11 UTC

    If you don't care about escaped " chars:

    perl -pi -e 's/source "(.+?)"/source $1/g'

    If you do:

    perl -pi -e 's/source "([^"\\]*(?:\\.[^"\\]*)*)"/source $1/g'

Re: remove a specific character occuring in file using script
by Anonymous Monk on Feb 10, 2005 at 09:05 UTC
    perl -pi -e 's|\"(\.\./version\.cmd)\"|$1|gi' a.txt --C