1

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?

pasaba por aqui
  • 3,446
  • 16
  • 40
  • 1
    Stack Overflow would probably be a better place for this question. – k170 Sep 02 '15 at 19:25
  • Agreed with above, but basically I think what you're doing wrong is putting the && outside of the *. I.e. you're matching everything then repeating, whereas you should be doing match, repeat, match, repeat. – Colm Bhandal Sep 02 '15 at 19:27
  • This looks like it is not quite appropriate for MSE. Meanwhile I suggest you consider using Perl e.g. the command "perl -pe 's/(\w)/\1\1/g;'" Perl can certainly do everything sed can and much more. –  Sep 02 '15 at 19:32

2 Answers2

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.

grand_chat
  • 431
  • 3
  • 3
1
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

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43