Simple awk script question

Hi,

I'm a total beginner at awk and hope someone can advise what I have done wrong in the following script:

I have a file which (to simplify things) may be something like this

Fred Smith and Sue Brown
Joe Jones and Jane Watts
Sally Green and Jim O?
Connor
Freda O?
Reiley and Pat O?
Connell
Peter Parker and Tina Williams

Notice some of the names are split. I need the file to look like this:

Fred Smith and Sue Brown
Joe Jones and Jane Watts
Sally Green and Jim O?Connor
Freda O?Reiley and Pat O?Connell
Peter Parker and Tina Williams

I had a go at writing the following:

cat input_file | awk '
BEGIN { totalline=""; flag=0 }
/\?\047$/ { totalline=totalline $0; flag=1 }
{ if (totalline == "" ) print $0 }
{ if (totalline != "" && flag == 0 ) print totalline, $0; totalline="" }
{ flag=0 }
' > output_file

But the output is

Fred Smith and Sue Brown
Joe Jones and Jane Watts
Connor
Connell
Peter Parker and Tina Williams

I'm not totally comfortable with awk syntax but I think I may not be adding the two strings together correctly.

Please help
Cheers
Helen :confused:

The following awk script works for me

#!/usr/bin/awk -f
{ if ( $0 ~ /\?$/ ) {
    line=line$0;
    next;
  } else {
     print line$0;
  }
  line="";
}

Call with ./myawkscript input_file > output_file

Tested on sample data gives

Fred Smith and Sue Brown
Joe Jones and Jane Watts
Sally Green and Jim O?Connor
Freda O?Reiley and Pat O?Connell
Peter Parker and Tina Williams

Cheers
ZB

Hi ZB

That works!

Many thanks again for your help
Cheers
Helen :wink: