ksh string replacement over a carriage return

Hi.

I need to make multiple string replacements in a file but several of the strings have been broken up by a carriage return so that the first few characters are on one line and the remainder on the following line.

E.g like the word 'post' in the following:

Use descriptive thread titles when posting. For example, do not po
st questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Po
st subjects like "Execution Problems with Cron"

I'm using a korn shell script with embedded perl. Should run on both linux and solaris. I tried the following:

#rm all carriage returns

perl  -0777 -nle 's/\n\s*( )/==$1/g; s/\n//g; s/==()/\n$1/g;  print "$_\n"' input.file > tmpfile

#replace the string

sed "s|string1|string2|g" tmpfile > input.file

#add carriage returns every 80 characters

perl -pi  -e's/^(.{79,79})(.*)/$1\n $2/' input.file

Seems to replace the string ok. However the format of the new output file is a bit messed up still when i try to put it back together again. Blank line are not handled for example. Not every line is cut to the correct length.

Any help greatly appreciated.
J

cat file | tr -d '\n' | sed 's/.\{79\}/&\n/g'

Blank lines are a sequence of two (or more) line feed chars. If you remove all of those, that info is lost.
Did you consider awk with its multi-line record feature?

thanks for the feedback guys.

I think perhaps my first step is not as it should be. If I use the following perl to merge a line with the one before it I get better formatting. It keeps the blank lines. However it does not keep lines that are less than 80 chars as they were. If I could get it to leave lines that are less than 80 chars alone it would mean only the code that needs to be changed is affected. So the last step of putting it back together would be simplified.

perl -ne '$a.=$_;END{$_=$a;s/  */ /g;s/[^\n]\n[^\n]/ /g;s/\n\n\n*/\n\n/g;print}' a.in > a.out

what do you think?

---------- Post updated 13-11-13 at 11:39 AM ---------- Previous update was 12-11-13 at 03:37 PM ----------

I thought I had the solution with the following but again the final file output contains lines longer than 79 characters. I'm operating on an ldif file so max line length and a space on the following line if it wraps around are essential.

           \# if a line begins with a space append it to previous line
sed -e :a -e '$!N;s/\n //;ta' -e 'P;D' ${ldif_file} > ${ldif_file}_tmp
            \# replace variable
sed "s|$string1|$tring3|g" ${ldif_file}_tmp  > ${ldif_file}_tmp1 && mv ${ldif_file}_tmp1 ${ldif_file}_tmp
            \# indent wrapped lines
perl -pi.bak  -e's/^(.{79,79})(.*)/$1\n $2/' ${ldif_file}_tmp
            \# backup input file and split lines over 80 chars long
fmt -s -w 80  ${ldif_file}_tmp > ${output_file}

Alas the ouput file format is still not correct. Any help would be greatly appreciated. Thanks again.