Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 06 Sep 2001 13:59:41 +0200
From:      Sheldon Hearn <sheldonh@starjuice.net>
To:        audit@FreeBSD.org
Subject:   mixing -l and -p in split(1) 
Message-ID:  <61597.999777581@axl.seasidesoftware.co.za>

next in thread | raw e-mail | index | archive | help

Hi folks,

I recently ran into a problem that would have been solved perfectly by
split(1) if said utility allowed the -l and -p options to be used in
conjunction.

The specific problem was the need to split a 4GB SMTP transcript into
4 files of approximately 1GB in size, to be passed as input into SMTP
socket connections.

The following patch allows -l and -p to be used together to mean

	Split the input into chunks of no fewer than line_count lines
	per chunk, but ensure that each chunk begins with a line that
	matches pattern.  If pattern does not appear regularly in the
	input, chunks may be considerably larger than line_count lines.

Obviously, it's pretty easy to shot yourself up the ass with this one,
but if you know what you're doing, it's invaluable.

Ciao,
Sheldon.

Index: split.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/split/split.c,v
retrieving revision 1.7
diff -u -d -r1.7 split.c
--- split.c	19 Nov 2000 01:44:20 -0000	1.7
+++ split.c	6 Sep 2001 11:18:02 -0000
@@ -147,7 +147,7 @@
 	if (*argv != NULL)
 		usage();
 
-	if (pflag && (numlines != 0 || bytecnt != 0))
+	if (pflag && bytecnt != 0)
 		usage();
 
 	if (numlines == 0)
@@ -238,14 +238,17 @@
 			goto writeit;
 
 		/* Check if we need to start a new file */
-		if (pflag) {
+		lcnt++;
+		if (pflag && lcnt >= numlines) {
 			regmatch_t pmatch;
 
 			pmatch.rm_so = 0;
 			pmatch.rm_eo = len - 1;
-			if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
+			if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0) {
 				newfile();
-		} else if (lcnt++ == numlines) {
+				lcnt = 1;
+			}
+		} else if (!pflag && lcnt == numlines) {
 			newfile();
 			lcnt = 1;
 		}

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?61597.999777581>