0

How to replace double consonants with only one letter using sed Linux command. Example: WILLIAM -> WILIAM. grep -E '(.)\1+' commands finds the words that follow two same consonants in a row pattern, but how do I replace them with only one occurrence of the letter?

I tried

cat test.txt | head | tr -s '[^AEUIO\n]' '?'
tripleee
  • 175,061
  • 34
  • 275
  • 318
Helen Grey
  • 439
  • 6
  • 16

2 Answers2

2

tr is all or nothing; it will replace all occurrences of the selected characters, regardless of context. For regex replacement, look at sed - you even included this in your question's tags, but you don't seem to have explored how it might be useful?

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

The dot matches any character; to restrict to only consonants, change it to [b-df-hj-np-tv-xz] or whatever makes sense (maybe extend to include upper case; perhaps include accented characters?)

The regex dialect understood by sed is more like the one understood by grep without -E (hence all the backslashes); though some sed implementations also support this option to select the POSIX extended regular expression dialect.

Neither sed not tr need cat to read standard input for them (though tr obscurely does not accept a file name argument). See tangentially also Useless use of cat?

tripleee
  • 175,061
  • 34
  • 275
  • 318
2

Match one consonant, remember it in \( \), then match is again with \1 and substitute it for itself.

sed 's/\([bcdfghjklmnpqrstvxzBCDFGHJKLMNPQRSTVXZ]\)\1/\1/'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111