问题
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 letterg
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