Duplicating characters using sed

六眼飞鱼酱① 提交于 2019-12-13 01:37:21

问题


I am working on trying to duplicate characters on certain words, but the sed script I wrote is not quite doing what I want it to. For example, I am trying to duplicate each character in a words like so:

FILE into FFIILLEE

I know how to remove the duplicate letters with :

sed s/\(.\)\1/\1/g' file.txt

This is the sed script I wrote, but it just ends up duplicating the whole word and I just get:

FILE FILE

This is what I wrote:

sed 's/[A-Z]*/& &/g' file.txt

How can I grab each letter and duplicate just the letter?


回答1:


A slight variation on your first script should work:

sed 's/\(.\)/\1\1/g' file.txt

Translation: For every character seen, replace it by itself followed by itself.




回答2:


sed 's/[[:alpha:]]/&&/g' file.txt
  • [:alpha:]class is the whole scope of letter available, you could extend with [:alnum:]including digit
  • & in replacement pattern is the whole search pattern matching. In this case 1 letter
  • g for each possible occurence

Your probleme was to use the * in search pattern that mean all occurence of previous pattern so the pattern is the whole word at once and not every letter of this word



来源:https://stackoverflow.com/questions/32361753/duplicating-characters-using-sed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!