From owner-freebsd-bugs Sat Jan 9 09:10:09 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id JAA17685 for freebsd-bugs-outgoing; Sat, 9 Jan 1999 09:10:09 -0800 (PST) (envelope-from owner-freebsd-bugs@FreeBSD.ORG) Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id JAA17676 for ; Sat, 9 Jan 1999 09:10:06 -0800 (PST) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.8.8/8.8.5) id JAA24418; Sat, 9 Jan 1999 09:10:00 -0800 (PST) Received: from frmug.org (frmug-gw.frmug.org [193.56.58.252]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id JAA17218 for ; Sat, 9 Jan 1999 09:02:21 -0800 (PST) (envelope-from charnier@xp11.frmug.org) Received: (from uucp@localhost) by frmug.org (8.9.1/frmug-2.3/nospam) with UUCP id SAA14258 for FreeBSD-gnats-submit@freebsd.org; Sat, 9 Jan 1999 18:01:42 +0100 (CET) (envelope-from charnier@xp11.frmug.org) Received: (from charnier@localhost) by xp11.frmug.org (8.9.1/8.9.1/xp11-uucp-1.1) id RAA03486; Sat, 9 Jan 1999 17:53:00 +0100 (CET) (envelope-from charnier) Message-Id: <199901091653.RAA03486@xp11.frmug.org> Date: Sat, 9 Jan 1999 17:53:00 +0100 (CET) From: Philippe Charnier Reply-To: charnier@xp11.frmug.org To: FreeBSD-gnats-submit@FreeBSD.ORG X-Send-Pr-Version: 3.2 Subject: bin/9405: split(1) when line matches a regex Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >Number: 9405 >Category: bin >Synopsis: split(1) when line matches a regex >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Sat Jan 9 09:10:00 PST 1999 >Closed-Date: >Last-Modified: >Originator: Philippe Charnier >Release: FreeBSD 3.0-CURRENT i386 >Organization: >Environment: >Description: New flag (-p pattern) that make split(1) capable of splitting a file when the analyzed line matches a given pattern. I first needed this change when making one-file-one-patch from a big diff -r: %split -p '^Index: ' big-patch-file patch- >How-To-Repeat: >Fix: Here is the patch to review, please look at correcting my english in the man page. Index: split.1 =================================================================== RCS file: /home0h/FreeBSD.cvsroot/src/usr.bin/split/split.1,v retrieving revision 1.1.1.1 diff -u -r1.1.1.1 split.1 --- split.1 1994/05/27 12:32:42 1.1.1.1 +++ split.1 1999/01/09 16:29:52 @@ -30,6 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)split.1 8.3 (Berkeley) 4/16/94 +.\" $Id$ .\" .Dd April 16, 1994 .Dt SPLIT 1 @@ -41,6 +42,7 @@ .Nm split .Op Fl b Ar byte_count[k|m] .Op Fl l Ar line_count +.Op Fl p Ar pattern .Op Ar file Op Ar name .Sh DESCRIPTION The @@ -70,6 +72,13 @@ Create smaller files .Ar n lines in length. +.It Fl p Ar pattern +The file is split when the analyzed line contains a match to the given +.Ar pattern . +Interpret +.Ar pattern +as an extended regular expression. See +.Xr regex 3 . .El .Pp If additional arguments are specified, the first is used as the name Index: split.c =================================================================== RCS file: /home0h/FreeBSD.cvsroot/src/usr.bin/split/split.c,v retrieving revision 1.4 diff -u -r1.4 split.c --- split.c 1997/08/11 07:30:22 1.4 +++ split.c 1997/08/17 10:45:30 @@ -44,6 +44,7 @@ #endif /* not lint */ #include +#include #include #include @@ -52,6 +53,7 @@ #include #include #include +#include #define DEFLINE 1000 /* Default num lines per file. */ @@ -61,6 +63,8 @@ int ifd = -1, ofd = -1; /* Input/output file descriptors. */ char bfr[MAXBSIZE]; /* I/O buffer. */ char fname[MAXPATHLEN]; /* File name prefix. */ +regex_t rgx; +int pflag; void newfile __P((void)); void split1 __P((void)); @@ -75,7 +79,8 @@ int ch; char *ep, *p; - while ((ch = getopt(argc, argv, "-0123456789b:l:")) != -1) + pflag = 0; + while ((ch = getopt(argc, argv, "-0123456789b:l:p:")) != -1) switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -108,6 +113,11 @@ else if (*ep == 'm') bytecnt *= 1048576; break; + case 'p' : /* pattern matching. */ + pflag = 1; + if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0) + errx(1, "%s: illegal regexp", optarg); + break; case 'l': /* Line count. */ if (numlines != 0) usage(); @@ -144,6 +154,8 @@ exit (0); } split2(); + if (pflag) + regfree(&rgx); exit(0); } @@ -220,18 +232,25 @@ newfile(); file_open = 1; } - for (Cs = Ce = bfr; len--; Ce++) - if (*Ce == '\n' && ++lcnt == numlines) { - bcnt = Ce - Cs + 1; - if (write(ofd, Cs, bcnt) != bcnt) - err(1, "write"); - lcnt = 0; - Cs = Ce + 1; - if (len) - newfile(); - else - file_open = 0; + for (Cs = Ce = bfr; len--; Ce++) { + if (*Ce != '\n') continue; + if (pflag) { + if (0 != regexec(&rgx, Ce + 1, 0, NULL, 0)) + continue; + } else { + if (++lcnt != numlines) + continue; } + bcnt = Ce - Cs + 1; + if (write(ofd, Cs, bcnt) != bcnt) + err(1, "write"); + lcnt = 0; + Cs = Ce + 1; + if (len) + newfile(); + else + file_open = 0; + } if (Cs < Ce) { bcnt = Ce - Cs; if (write(ofd, Cs, bcnt) != bcnt) @@ -284,6 +303,6 @@ usage() { (void)fprintf(stderr, -"usage: split [-b byte_count] [-l line_count] [file [prefix]]\n"); +"usage: split [-b byte_count] [-l line_count] [-p pattern] [file [prefix]]\n"); exit(1); } >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message