Issue
I have the following text file:
(ABC)
(DEF)
(GHI)
...
I want to use sed
to change it to:
(ABCABC)
(DEFDEF)
(GHIGHI)
...
So I'm trying to match a sub-pattern of each line and access the variable in the replace pattern.
Obviously my failed attempt sed 's/(.*)/&&/' file.txt
duplicates the whole line.
Solution
You may use:
sed -E 's/\(([^)]+)\)/(\1\1)/g' file
(ABCABC)
(DEFDEF)
(GHIGHI)
Details:
-E
: Enable extended regex mode\(
: Match a literal(
([^)]+)
: Match 1+ of any character that is not)
and capture in group #1\)
: Match a literal)
\1\1
:\1
is back-reference of group #1 and by using\1\1
we get repeat of matched group #1
Answered By - anubhava Answer Checked By - Senaida (WPSolving Volunteer)