-1

I need change in a terminal all occurrences of the string path "/../path/file.extension" to "/path/file.extension" of my file using a sed command.

Notice that i just need replace "/../" to "/".

I have been tried something like this, but not work: sed -i \"s/..\//\//g\" myfile.php

edit

This config is for use in a package.json and I have some limitations beacuse this.

enter image description here

3
1

How about just replacing beginning /.. with blank.

echo "/../path/file.extension" | sed 's|/..||'
4
  • That would remove the three first characters of any absolute path.
    – Kusalananda
    Jan 31 '19 at 7:50
  • works, but sed 's|/..||' ../../myfile.php is not saving the file. and the command sed -i 's|/..||' ../../myfile.php returns me sed: 1: "../../myfile.php": invalid command code . Jan 31 '19 at 11:42
  • You solution removes all comments in my code // my comment Jan 31 '19 at 11:56
  • Your solution helps me to find a correct answer. I change my config in the package.json file to "changeIndexFile": "sed -i -e \"s|'/..|'|\" ../../indecente-api/index.php", and now it's working great. Basically the command pick all the occurrences starts with '/ and remove it with two more right chars and adds ' the "hack" to ' was necessary because your solution remove the comments code. Jan 31 '19 at 12:09
1

You can use:

echo '/../path/file.extension' | sed -e 's@/\.\.@@g'

It will give output /path/file.extension. It replaces /.. with blank.

Or if you are using some file then you can use:

sed -i 's@/\.\./path/file\.extension@/path/file.extension@g' myfile.php
5
  • No works. the file package.json dosen't accept the escape \. Jan 31 '19 at 11:27
  • @FabianoLothor I didn't get what you want to say?
    – Prvt_Yadav
    Jan 31 '19 at 11:28
  • I'm go update my question. Jan 31 '19 at 11:32
  • I update the question. Jan 31 '19 at 11:37
  • I solve the problem, thanks for you help! Jan 31 '19 at 12:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.