I have a file with following contents..\..\src\modules\core\abc\abc.cpp..\..\src\modules\core\something\xyz\xyz.cpp..\..\src\other_modules\new_core\something\pqr\pqr.cpp..\..\src\other_modules\new_core\something\pqr\abc.cppThe result I am expecting is ..\..\src\abc\abc.cpp..\..\src\xyz\xyz.cpp..\..\src\pqr\pqr.cpp..\..\src\pqr\abc.cppHow can I achieve this using sed?I am unable to write an regular expression to capture two groups at the same time.initial group (....\src) - this will be same in all the linesvariable group (abc\abc.cpp) or (xyz\xyz.cpp) or (pqr\pqr.cpp) or (pqr\abc.cpp) 
Delete string between two regex patterns
text processing;sed
With BSD sed or recent versions of GNU sed (for older versions, replace -E with -r):sed -E 's#(.*\\src).*(\\[^\]+\\[^\]+$)#\1\2#' file.txt# is used as the delimiter for substitution (s) command of sed, to avoid ambiguity involving \s in the input(.*\\src) matches upto src from start, and put the match in captured group 1(\\[^\]+\\[^\]+$) matches the portion having two \s till end, and put in captured group 2, the .* preceding this matches everything in between the first and second captured groupsIn the replacement we have used the two captured groupsPOSIX-ly:sed 's#\(.*\\src\).*\(\\[^\]\+\\[^\]\+$\)#\1\2#' file.txtExample:% cat file.txt..\..\src\modules\core\abc\abc.cpp..\..\src\modules\core\something\xyz\xyz.cpp..\..\src\other_modules\new_core\something\pqr\pqr.cpp..\..\src\other_modules\new_core\something\pqr\abc.cpp% sed -E 's#(.*\\src).*(\\[^\]+\\[^\]+$)#\1\2#' file.txt..\..\src\abc\abc.cpp..\..\src\xyz\xyz.cpp..\..\src\pqr\pqr.cpp..\..\src\pqr\abc.cpp