操作系统:Debian Jessie 64
sed(GNU sed)4.2.2
I have to replace my path from ../ to ../../
Testing file.
$ cat dot.js
path = '../'
Run Code Online (Sandbox Code Playgroud)
I put backslash in front of slash to put in the single quote:
sed -i 's/..\//..\/..\//g' dot.js
Run Code Online (Sandbox Code Playgroud)
Output :
path = '../../'
Run Code Online (Sandbox Code Playgroud)
However, when I use with my actual file.
$ cat dotdot.js
var real_questionHandler = require('../routes/questionHandler_delqid.js');
var questionHandler_delqid = proxyquire('../routes/questionHandler_delqid.js', { 'pg' : pgStub} );
Run Code Online (Sandbox Code Playgroud)
Output :
var real_questionHandler = require('../../rout../../questionHandler_delqid.js');
var questionHandler_delqid = proxyquire('../../rout../../questionHandler_delqid.js', { 'pg' : pgStub} );
Run Code Online (Sandbox Code Playgroud)
How can I protect second position of each line from replacement?
You need to escape all the dots so that it would match the literal dot character or otherwise , unescaped dot would match any character. So that your regex matches ../ as well as es/
sed -i 's/\.\.\//..\/..\//g' dot.js
Run Code Online (Sandbox Code Playgroud)
or
sed -i 's~\.\./~../../~g' dot.js
Run Code Online (Sandbox Code Playgroud)
Example:
$ echo "require('../routes/questionHandler_delqid.js');" | sed 's~\.\./~../../~g'
require('../../routes/questionHandler_delqid.js');
Run Code Online (Sandbox Code Playgroud)