using sed to replace two patterns within a larger pattern
Solution 1:
This command converts your input example to your output example:
sed 's|\\|/|g;s|^[^/]*|/enlistments|'
If some of your input contains backslashes in the portion after the /^.*([[:digit:]]+):/
part, then you will have to divide and conquer to prevent those latter backslashes from being replaced.
sed 'h;s/^.*([[:digit:]]\+)://;x;s/^\(.*([[:digit:]]\+):\).*/\1/;s|\\|/|g;s|^[^/]*|/enlistments|;G;s/\n//'
Explanation (steps marked with an asterisk ([*]
) apply to both commands):
-
h
- copy the line to hold space -
s/^.*([[:digit:]]\+)://
- delete the first part of the line from the original in pattern space -
x
- swap pattern space and hold space -
s/^\(.*([[:digit:]]\+):\).*/\1/
- keep the first part of the line from the copy (discard the last part) -
s|\\|/|g
- [*
] change all the backslashes to slashes (in the divide-and-conquer version, only the portion in pattern space - the first part of the line - is affected) -
s|^[^/]*|/enlistments|
- [*
] change whatever appears before the first slash into "/enlistments" - this could be made more selective if needed -
G
- append a newline and the contents of hold space onto the end of pattern space -
s/\n//
- remove the interior newline