简体   繁体   中英

sed replace dot dot slash, but e s slash got replace

OS : Debian Jessie 64
sed (GNU sed) 4.2.2

I have to replace my path from ../ to ../../
Testing file.

$ cat dot.js 
path = '../'

I put backslash in front of slash to put in the single quote:

sed -i 's/..\//..\/..\//g' dot.js

Output :

path = '../../'

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} );

Output :

var real_questionHandler = require('../../rout../../questionHandler_delqid.js');
var questionHandler_delqid = proxyquire('../../rout../../questionHandler_delqid.js', { 'pg' : pgStub} );

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

or

sed -i 's~\.\./~../../~g' dot.js

Example:

$ echo "require('../routes/questionHandler_delqid.js');" | sed 's~\.\./~../../~g'
require('../../routes/questionHandler_delqid.js');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM