After writing this answer, I did some googling to find out if I could make the sed y//
command be applied only to the match and not to the entire line. I wasn't able to find anything relevant.
$ echo clayii | sed -e '/clayii/ y/clayk/kieio/'
kieiii
In other words, if the search word (clayii) is just one of many words in the input line, I want the y//
command to be applied only to that word and not to the remainder of the line.
i.e. I don't want this:
$ echo can sed ignore everything but the matching word - clayii |
sed -e '/clayii/ y/clayk/kieio/'
ken sed ignore everithing but the metkhing word - kieiii
Is that possible in sed
? or do i need to use something more capable like perl
?
Best Answer
No, the
y
command applies to all matching characters in the pattern space. Per the POSIXsed
documentation (emphasize mine):OSX/BSD
man
page:and
GNU sed info
page:Sure, you could use the hold buffer to save current pattern space then retain only the match, transliterate and restore the pattern space replacing the initial match with the result e.g. compare
with
but as soon as you start adding patterns/requirements it gets complicated.