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.
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.
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.
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.