Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you join (concatenate) variable number of lines in a file 1

Status
Not open for further replies.

keusch

Technical User
Jun 17, 2001
41
US
I can't seem to hurtle creating a file with variable number of joins (line concatenation) - I can do this in a macro in vi but haven't mastered joining in an awk script with more than one line joining at a time.
the script needs to:

search for ^STARTRECORD and append each line following that begins with ^ JOINRECORD

INPUT

STARTRECORD 49035063810000 PT 001 OIL ISIP 5900
JOINRECORD MIN/ 5800
STARTRECORD 49035063810000 PT 002 OIL, ISIP 5700
STARTRECORD 49035063810000 PT 005 GAUGES NOT MCFD-1540 MCFD
STARTRECORD 49035063810000 PT 003 FLOWED
JOINRECORD 887 BY END OF TEST, STABALIZED AT 768 MCFD
JOINRECORD ACT W/GEL DIESEL-FLUSHED W/9660 GALS CLEAR DIESEL ISIP
JOINRECORD 5200 SIP /15 MINS/ 5000 FLOWED 1020 MCFD DECR TO 362 MCFD

OUTPUT SHOULD LOOK LIKE:
STARTRECORD 49035063810000 PT 001 OIL ISIP 5900 JOINRECORD MIN/ 5800
STARTRECORD 49035063810000 PT 002 OIL, ISIP 5700
STARTRECORD 49035063810000 PT 005 GAUGES NOT MCFD-1540 MCFD
STARTRECORD 49035063810000 PT 003 FLOWED JOINRECORD 887 BY END OF TEST, STABALIZED AT 768 MCFD JOINRECORD ACT W/GEL DIESEL-FLUSHED W/9660 GALS CLEAR DIESEL ISIP JOINRECORD 5200 SIP /15 MINS/ 5000 FLOWED 1020 MCFD DECR TO 362 MCFD

This is a simplified file. In reality, I will not need the STARTRECORD AND jOINRECORD IN THE FINAL FILE; HOWEVER, IT WAS EASIER FOR ME TO SCRIPT IT IN AT THIS POINT. ( I was using sed to strip the final file)
Thanks,
And while I'm at it - How about a good "basics" awk book?
Thanks again


 
Hi keusch,

Your output file looks amazingly like your input file. I don't know if it is a cut and paste problem or a formatting problem with the forum. This may not do exactly what you want but should get you started.
Code:
{
  if ($1 == "STARTRECORD") {
    if (flg) print ""
    printf substr($0,12)
    flg = 1
  }
  else if ($1 == "JOINRECORD" && flg) {
    printf substr($0,11)
  }
  else {
    if (flg) print ""
    print
    flg = 0
  }
}
END {
  if (flg) print ""
}
Hope this helps. CaKiwi
 
Hi:

Sed & Awk is a very good book, but I still use The AWK Programming Language by Aho, Weinberger, and Kernigan.

It's old, but still is available at Amazon.com.

Regards,

Ed
 
This might save you having to add the STARTRECORD and JOIN RECORD.

/BEGIN/ { FLAG = 0 }
/^49035063810000/ { if ( FLAG )
printf("\n")
else
FLAG = 1
printf("%s",$0)
}
!/^49035063810000/ { printf("%s",$0) }

The AWK Programming Language by Aho, Weinberger, and Kernigan, is also an excellent book and thin.
 
THANKS FOR EVERYONE'S HELP.
As stated in the original statement, this was a very simple example of a large file. Misercord, your 1st suggestion worked great. Your second example of was not applicable since the string 4903506381 was a variable and changed throughout the 47,000 line file. I 'm still trying understand the finer points of your second sugestion as far as how it is set up. I may have to get back with you on that one.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top