in reply to Re: how to modify the text in file
in thread ho w to replace a occurence of string by other string

i was using something like perl -npi.bak -e "s:(\w\d+\.data):testname/$1:g" file1.txt . it actually repleces the text and do not do what i wanted

Replies are listed 'Best First'.
Re^3: how to modify the text in file
by dse (Novice) on Jan 19, 2005 at 07:31 UTC
    1. First, you need to replace "\w\d+" with "\w+\d+". Otherwise, your regex is only going to match "x1.data" instead of "xxx1.data".
    2. Second, I assume you're using bash, sh, or a similar shell on a Unix box. When passing arguments using double-quotation marks, you need to escape any backslashes, dollar signs, and certain other characters. I recommend using single-quotation marks instead since most characters don't have to be escaped inside them.

    Suggestion:

    perl -npi.bak -e 's:(\w+\d+\.data):testname/$1:g' file1.txt
    
Re^3: how to modify the text in file
by Thilosophy (Curate) on Jan 19, 2005 at 07:30 UTC
    Aha !
    perl -npi.bak -e "s:(\w\d+\.data):testname/$1:g" file1.txt
    That is a shell quoting problem. Your $1 gets replaced by an (empty) environment variable before Perl sees it. Use single quotes:
    perl -npi.bak -e 's:(\w\d+\.data):testname/$1:g' file1.txt