🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.lang.awk
Article #30

AWK: split multiple messages from a mail file

#30
From: william E Davidsen
Date: Tue, 02 Mar 1993 16:53
76 lines
2328 bytes
  Someone asked the other day for a program to split shar files, or
asked for a repost, or some such. This is a little awk program to do the
job, one I use regularly, since I tend to dump all the parts of a source
in one file and fight with it later.

  I tried to post this a few days ago, and it didn't *seem* to go out so
I'm reposting. If you've seen it before and you're ungenerous, please
flame by mail.

# splitshar - split a multipart posting into separate parts again
#
# call with
#	awk -f splitarch.awk <file>

BEGIN {
  filenum = 0
  line1 = 1
  header = "splitarch.hdr"
  outfile = header
  filename = outfile
  pending = 0			# have no archive pending
}

line1 == 1 {
  # save the "start of article" string"
  matchstr = $1
  line1 = 0
}

$1 == matchstr {
  if (filenum) {
    close(filename)
    printf(" %d lines\n", Lines);
  }
  # go back to the hdr file
  filename = header
  filenum++
  pending = 0
}

# find an Archive-name: header line
/^Archive-name:/ {
  pending = 1
  p_filename = $2
  dir = p_filename
  sub("/.*$", "", dir)
  printf("File: %s", p_filename)
}

# Here's where we pick up the end of header
/^ *$/ {
  if (pending) {
    pending = 0
    filename = p_filename
    Lines = 0
  }
}

# this is the default action, for all lines
{
  print > filename
  ++Lines
}

END {
  printf(" %d lines\n", Lines)
  if (filenum == 1) print "It was only one file"
  else printf("Saved in %d files\n", filenum)
  close(outfile)
  printf("mv %s %s/\nls -l %s", header, dir, dir) | "sh"
}

-- 
bill davidsen, GE Corp. R&D Center; Box 8; Schenectady NY 12345
    Windows NT is a *great* program!
    It's everything CP/M should have been all along.
Technical Details
Message-ID: <1993Mar2.165300.16491@crd.ge.com>
Path: arc1-ca.pugleaf.net!wupost!howland.reston.ans.net!zaphod.mps.ohio-state.edu!rpi!news.crd.ge.com!rdsunx.crd.ge.com!ariel!davidsen

Show Full Headers
Complete Article Headers
Newsgroups: alt.lang.awk,alt.sources
Path: arc1-ca.pugleaf.net!wupost!howland.reston.ans.net!zaphod.mps.ohio-state.edu!rpi!news.crd.ge.com!rdsunx.crd.ge.com!ariel!davidsen
From: davidsen@ariel.crd.GE.COM (william E Davidsen)
Subject: AWK: split multiple messages from a mail file
Message-ID: <1993Mar2.165300.16491@crd.ge.com>
Followup-To: alt.sources.d,alt.lang.awk
Sender: usenet@crd.ge.com (Required for NNTP)
Nntp-Posting-Host: ariel.crd.ge.com
Reply-To: davidsen@crd.ge.com (bill davidsen)
Organization: GE Corporate R&D Center, Schenectady NY
Date: Tue, 2 Mar 1993 16:53:00 GMT
Lines: 76