s[\w\w\w1\.data][$testname/]g
you might consider s[(\w\w\w1\.data)][$testname/$1]g
instead.
The $1 stands for the text enclosed in the first set of round brackets in the pattern. | [reply] [d/l] [select] |
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
| [reply] |
- First, you need to replace "\w\d+" with "\w+\d+". Otherwise, your regex is only going to match "x1.data" instead of "xxx1.data".
- 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
| [reply] |
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
| [reply] [d/l] [select] |