Striping does not match expected in sed

Studying sed, I want to change the drive letter of the following input lines from 'a' to 'd':

http:/a/foo/bar.txt
http:/a/bar/foo.txt
file:/a/foobar.txt
http:/b/foo/bar.txt
http:/c/foobar.txt

I use the following sed expression:

sed 's_^\(http|file\):/a_\1:/d_' in.txt

That is: if the line starts with "http" or "file", then use :: / a to capture the protocol as group 1 and replace "a" with "d". Or at least it should be.

However, I cannot force the interleave operator '|' work. If I just use "http" or "file" for myself for the group, the command behaves as expected, if I use "http | file" for the group, then none of the lines matches:

mbook:sed rob$ sed 's_^\(http\):/a_\1:/d_' in.txt
http:/d/foo/bar.txt
http:/d/bar/foo.txt
file:/a/foobar.txt
http:/b/foo/bar.txt
http:/c/foobar.txt
mbook:sed rob$ sed 's_^\(file\):/a_\1:/d_' in.txt
http:/a/foo/bar.txt
http:/a/bar/foo.txt
file:/d/foobar.txt
http:/b/foo/bar.txt
http:/c/foobar.txt
mbook:sed rob$ sed 's_^\(http|file\):/a_\1:/d_' in.txt
http:/a/foo/bar.txt
http:/a/bar/foo.txt
file:/a/foobar.txt
http:/b/foo/bar.txt
http:/c/foobar.txt
+3
source share
2 answers

|

sed -r 's_^(http|file):/a_\1:/d_'
+2

:

sed 's_^\(http\|file\):/a_\1:/d_' in.txt

: sed regex.

+4

All Articles