Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a text file that consists of some N ORMAL E NGLISH W ORDS in all-caps. Unfortunately, these words all have a space between the first and following letters, which is not acceptable. I tried writing a sed expression that would match these occurrences sed 's/[A-Z] [A-Z][A-Z]*/, and I was successful. However, I was not successful in writing the replacement part.

share|improve this question
What is your final objective? Are the space matching a specific pattern? Because if it's just random space noise, you can't expect sed to "recognise" English words. – M'vy Jul 8 '11 at 9:52
up vote 6 down vote accepted

You would need to group the two parts of the word and use them as backreferences in the replacement like this:

sed -e 's/\([A-Z]\) \([A-Z]\+\)/\1\2/g'

Note that I also changed your use of * to + to indicate 1 or more occurances of the previous character instead of 0 or more, saving a little step.

Also you might have problems with this whole expression if the word 'I' is found by itself. Do you have anything else you can match on to know whether an I should be it's own word or joined to the next? What about 'A' and other single letter words?

I recommend combining this simple text hack with some kind of spell-check so that to help check for mistakes made by the replace such as "ABIRD" or "IWENT". No amount of regex fiddling is going to give you a perfect result on a large corpus of broken text like you describe, but once you've done some rough cleanup with regex you can go on to finer-tuned methods like spell checkers (which can also be run from the command line and automated) to clean things up. Finally, have a human proof-read it!

share|improve this answer
great answer. thank you. – ixtmixilix Jul 8 '11 at 11:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.