you can loop over it:
echo here is a pat and a tern and a pattern |
sed -e'1{H;x;s/\(.\).*/\1pattern\1replace/;x;}' \
-eG -e'/\(.*\)\(.*\n\)\(\n\1\)\n/!{P;d;}' \
-e's//\3\2/;t-' \
-e:- -e's/\(\n\)\(.\)\(.*\n\)\(.\)/\4\1\3/;t-' \
-e's/\n//;P;d'
here is a pat and a tern and a replace
That does the replacement char by char progressing left to right. It shifts the first delimiter to the right of the replaced character for each substitution. You can see what I mean if you stick a l
ook command in just before the loop t
ested substitution after -e:-
:
here is a pat and a tern and a \npattern\nreplace$
here is a pat and a tern and a r\nattern\neplace$
here is a pat and a tern and a re\nttern\nplace$
here is a pat and a tern and a rep\ntern\nlace$
here is a pat and a tern and a repl\nern\nace$
here is a pat and a tern and a repla\nrn\nce$
here is a pat and a tern and a replac\nn\ne$
here is a pat and a tern and a replace\n\n$
here is a pat and a tern and a replace
If you really are looking for a sort of translation thing, you can do that, too. I wrote this earlier as an answer to another question:
It looks doable - you just have to sort of shift-in/shift-out:
echo can ccccc ccccccccclayii sed clay ignore \
every cclayii thing but the matching word\
- cclayiicclayii |
sed -e'y/ /\n/' \
-eh -e's/\(cclayii\)\1*/ & /g;x;s// /g;s/^/ /' \
-ex -e's//./;s/\([^ ]* *\)\{2\}/\1 /g;s/^/ /' \
-e'y/clayk/kieio/;G;t$' -e:$ \
-e'/^ \n /{s///;y/ \n/\n /;}' \
-et -e's/^ *\([^ ]*\) \(.* \n [^ ]*\) /\2\1/;t$'
can ccccc ccccccckkieiii sed clay ignore every kkieiii thing but the matching word - kkieiiikkieiii
...it isn't easy, though.
That one loops a little bit, too, but not nearly as much.
As is true of most complicated problems, though, it is way easier if you use two sed
s:
echo can ccccc ccccccccclayii sed clay ignore \
every cclayii thing but the matching word\
- cclayiicclayii |
sed -e's/\(cclayii\)\1*/\n&\n /g;G;s/^/ /'|
sed -e'/^ /!y/clayk/kieio/;/./{H;d;}' \
-e'x;s/\n \{0,1\}//g'
can ccccc ccccccckkieiii sed clay ignore every kkieiii thing but the matching word - kkieiiikkieiii
With a BSD sed
you'll want to use a literal newline in place of the n
in the \n
escapes in the right-hand substitution for the first sed
there.
-E
and don't worry about BSD sed.sed
to replace individual letters, then don't tell it to do that. either search and replace entire words, or restrict the s/// command to operate only on matching words.