need to create lines from ones that begin with the field separator

Hello,

I need to modify an awk script to recognize the last field $NF when the line is split over more than 1 line.

In my input file the field separator is the exclamation mark ! so FS="!"

So here is my input file infile.txt, it has 2 records, the field separator is in bold:

INPUT

4888689!?!Change of PROD
!TX!?!DETAILS
!US!?!?!file_937_20101024_065520.txt 

48887370!Change of PROD!?!?!cmt_det3_937_20101024_065520.txt

Before I do anything in the AWK script I need to be able recognize the last field for each line type.

For record type #1 the $NF (last field) is on a different line (I need to be able to get the last field for these) (AWK recognizes record one as being 3 different lines) ($NF would be file_937_20101024_065520.txt)

For record type #2 the $NF (last field) is on the same line (this is the way AWK would read most of them) ($NF would be cmt_det3_937_20101024_065520.txt)

So the rule would be:

If the field separator is the 1st character on the line then look at the previous line until the line does not start with the field separator and so then
$1 would be on the first line and the $NF will be at the end of the last line.

Then print the lines to an output file

OUTPUT

4888689!?!Change of PROD!TX!?!DETAILS!US!?!?!file_937_20101024_065520.txt 

48887370!Change of PROD!?!?!cmt_det3_937_20101024_065520.txt
Input file :test has your text

bash-3.2$ cat test  | tr -d '\n'  |sed 's/txt/txt#/g' |tr '#' '\n'
4888689!?!Change of PROD!TX!?!DETAILS!US!?!?!file_937_20101024_065520.txt
 48887370!Change of PROD!?!?!cmt_det3_937_20101024_065520.txt

I gave a try but this is not a good way to do ..
hope some else helps you...

Try this:

awk 'NR==1{s=$0; next}
/^!/{s=s $0;next}
{print s; s=$0}
END{if(s) print s}' file

Thanks for the bash but I need to do it in AWK.

I put the code in a file called test.awk:

awk 'NR==1{s=$0; next}
/^/{s=s $0;next}
{print s; s=$0}
END{if(s) print s}' test_file.txt

On the UNIX Command Line I type:

./test.awk > new_file.txt

I get this error:

syntax error The source line is 1.
 The error context is
 <<<            NR==1{s=$0; >>>  next}
 awk: Quitting
 The source line is 1.

Use nawk or /usr/xpg4/bin/awk on Solaris.

Why does it work on those?

---------- Post updated at 04:51 PM ---------- Previous update was at 04:44 PM ----------

I think I need to use the sh environment..

From the UNIX Command Line I type:

./t.awk > 
new_file.txt

I get the error message:

interpreter "/usr/bin/sh" not found
ksh: ./t.awk:  not found

What kind of script is t.awk?

Can you post the entire script?