Merging words splitted into characters with awk

I have an OCR output with some words splitted into single characters separated by blank spaces,
and I want the same text with these words written correctly.

Example:
This is a text w i t h some s p l i t e d W o r d s .

The regular expression for matching splitted words could be something like this (I'm not so much worried about that):

grep -E "([A-Z])?( [a-z]){2,100} [.,]?"

My question is:
Once I've matched the string, how can I delete this annoing blank spaces?
I tried with the awk gsub and gensub functions but I'm not so hard with this.

I just reach to do this:

awk '{ S=gensub(" ([a-z]) ([a-z]) ([a-z]) ", " \\1\\2\\3 ", "g", $0); print S}'

That is not sufficient at all: the number of splitted characters is always different (not 3!)

Which is the right way to do this? any help? Thanks

This may get you started:

awk '
{
  while(match($0,/ [^ ] [^ ]( [^ ])+ /)>0) {
    x = substr($0, RSTART, RLENGTH)
    gsub(/ /, "", x)
    $0 = substr($0,1,RSTART) x substr($0,RSTART+RLENGTH-1)
  }
  print
}'

dokamo,

Working based on your input example, the better solution I get so far I�ve divided in 4 sed parts for better understanding, you can try the "echo" followed by one sed command at a time to see what it does each one.

The problem is when a splitted word is followed by another splitted word, in this case, in the output, both words appear joined.

If it is close what you want, you only need to join 4 sed parts in a unique sed command.

echo " This is a text w i t h some s p l i t e d W o r d s ." | 
sed 's/\([a-z][a-z]?*\)\( \)/\1|/g' | 
sed 's/\([a-z]\)\( \)\([a-z][a-z]\)/\1|\3/g' | 
sed 's/ //g' | 
sed 's/|/ /g'
This is a text with some splitedWords.

Hope it helps.

Regards

echo "This is a text w i t h some s p l i t e d W o r d s ." |
awk '{a[NR]=$1;b[NR]=length($1)}
 
 END{
       for(i=1;i<=NR;i++) 
       {
         if(b>1) {printf a" "} 
         else if (b==1 && a~/[aA]/ && b[i-1]>1 && b[i+1]>1) {printf a" "}  
         else if (b==1 && b[i-1]>1 && b[i+1]==1) {printf " "a} 
         else if (b==1 && b[i-1]==1 && b[i+1]>1){printf a" "} 
         else {printf a}
        }
      }' RS=" " |
tr -s " "
This is a text with some splitedWords.

Oh, yes!
I will adjust the script in order to match the "most" cases as possible and then I will post it here.
Thank you all!

Ok, this script matches my case:

awk '
BEGIN{
	q='"\"'\""'; 
	o=""
}
{
	for(i=1;i<=NF;i++)
	{
		f=$i;fl=length($i)
		nf=$(i+1);nfl=length($(i+1))
		if (nfl==0) {o=o f}
		else if(fl>1) {o=o f" "} 
		else if ((fl==1) && (nfl>1)) {o=o f" "}
		else if ((f~/[[:lower:]]/) && (nf~/[[:upper:]]/ || nf~/[[:digit:]]/ || nf~/[\(]/) ) {o=o f" "} 
		else if ((f~/[[:lower:]]/) && (nf~/[[:digit:]]/ ) ) {o=o f" "} 
		else if ((f~/[[:digit:]]/) && (nf~/[[:alpha:]]/)) {o=o f" "}
		else if ((f~/[[:punct:]]/) && (f!~/[\(\-]/ && f!=q && nf!~/[[:punct:]]/)) {o=o f" "}
		else {o=o f}
	} 
	o=o RS
	printf "%s", o > output.txt
	o=""
}
END{

}' input.txt

Of course this can't keep separated two lower/upper case words.
Fortunetly, my ocr text is full of punctuation and capitalizations, so that the result was good enougth for my aim.