Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ukma!husc6!ogccse!littlei!omepd!merlyn From: merlyn@intelob.intel.com (Randal L. Schwartz @ Stonehenge) Newsgroups: comp.lang.c Subject: FIXED Perl line-scruncher solution (was Re: Scrunch blank lines) Summary: arrrgh Keywords: messedup Message-ID: <4260@omepd.UUCP> Date: 30 Mar 89 21:59:43 GMT References: <7150@siemens.UUCP> <9900010@bradley> <4896@cbnews.ATT.COM> <26389@cornell.UUCP> <6839@cg-atla.UUCP> <623@gonzo.UUCP> <4257@omepd.UUCP> Sender: news@omepd.UUCP Reply-To: merlyn@intelob.intel.com (Randal L. Schwartz @ Stonehenge) Distribution: na Organization: Stonehenge; netaccess via BiiN, Hillsboro, Oregon, USA Lines: 39 In-reply-to: myself :-) In article <4257@omepd.UUCP>, a rather dingy merlyn@intelob writes: | Here's my solution, just 5 lines of (mostly readable :-) Perl... | | #!/usr/bin/perl | for ($file = "stdin", $line = 1; $_ = ;) { | ($line = $1, $file = $2, $sync = 0, next) if /^#\W*(\d+)\W+"(.*)"\W*$/; | ($sync = 0, $line++, next) if /^\W*$/; | printf "#line %d \"%s\"\n", $line, $file unless $sync++; | print; $line++; | } Arrrgh. Someone should beat into my head that non-word != whitespace. All the '\W' up there should be '\s'. Anyway, for those of you that want the "new and improved" version, try this (I call it "seepp"): ---------------------------------------- cut here -------------------- #!/usr/bin/perl open(CPP,$a = "/lib/cpp " . join(" ",@ARGV) . "|") || die "Cannot exec '$a' ($!)\n"; for ($file = "/dev/null", $line = 1; $_ = ;) { ($file = $1, $line = $2, $sync = 0, next) if /^#\s*"(.*)"\s+(\d+)\s*$/; ($file = $1, $line++, $sync = 0, next) if /^#\s*"(.*)"\s*$/; ($line = $1, $file = $2, $sync = 0, next) if /^#\s*(\d+)\s+"(.*)"\s*$/; ($line = $1, $sync = 0, next) if /^#\s*(\d+)\s*$/; ($sync = 0, $line++, next) if /^\s*$/; printf "#line %d \"%s\"\n", $line, $file unless $sync++; print; $line++; } close(CPP) || die "Cannot close '$a' ??? ($!)\n"; exit(0); ---------------------------------------- cut here -------------------- (side note to Larry: if you wanna stick this in 'eg', go ahead...) Probably should have had lunch first, -- / Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 \ | on contract to BiiN (for now :-) Hillsboro, Oregon, USA. | |<@intel-iwarp.arpa:merlyn@intelob.intel.com> ...!uunet!tektronix!biin!merlyn | \ Cute quote: "Welcome to Oregon... home of the California Raisins!" /