-1

I have to find and replace all the occurrence of a string in all files /eOffice/eofficev6 to /eOffice/SAPS/eofficev6 recursively in a directory in RHEL 7.4.

Problem is that I using sed -i but my string also containing / slash.

How to replace all string having /?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • change `sed` subcommand delimiter `sed~/eOffice/eofficev6~/eOffice/SAPS/eofficev6~g` – RomanPerekhrest May 12 '18 at 11:24
  • This might help: [Escaping forward slashes in sed command](https://stackoverflow.com/a/40715028/3776858) – Cyrus May 12 '18 at 11:25
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 12 '18 at 13:45
  • Possible duplicate of [sed search and replace strings containing /](https://stackoverflow.com/q/10309968/608639), [How to replace strings containing slashes with sed?](https://stackoverflow.com/q/16790793/608639), [How to use different delimiters for sed substitute command?](https://stackoverflow.com/q/5864146/608639), etc. – jww May 13 '18 at 02:54

2 Answers2

0

You could do

sed -i 's/\(\/eOffice\)\(\/eofficev6\)/\1\/SAPS\2/' input_file_name

The "/eOffice" and "eofficev6" parts are grouped and the "SAPS" is inserted in between.

For example, if the input is:

/eOffice/eofficev6

the output will be

/eOffice/SAPS/eofficev6

Forward slashes are escaped with \s.

Or without grouping just,

sed -i 's/\/eOffice\/eOfficev6/\/eOffice\/SAPS\/eOfficeb6/' input_file
J...S
  • 5,079
  • 1
  • 20
  • 35
  • 1
    I would be glad to identify my mistakes and learn. Any comments on why this answer is bad? – J...S May 14 '18 at 12:41
  • I think this answer is more than exaustive! I didn't know about groups [: @ OP: You should flag this answer as the right one! – ingroxd May 29 '18 at 08:48
0

You have two ways to achieve what you want.

One: Escape slashes (E.G. sed -i "s/\/eOffice\/eofficev6/\/eOffice\/SAPS\/eofficev6/" file).

Two: change the delimiter (E.G. sed -i "s|/eOffice/eofficev6|/eOffice/SAPS/eofficev6|" file).

ingroxd
  • 995
  • 1
  • 12
  • 28