FIXED Perl line-scruncher solution (was Re: Scrunch blank lines)
Randal L. Schwartz @ Stonehenge
merlyn at intelob.intel.com
Fri Mar 31 07:59:43 AEST 1989
In article <4257 at omepd.UUCP>, a rather dingy merlyn at intelob writes:
| Here's my solution, just 5 lines of (mostly readable :-) Perl...
|
| #!/usr/bin/perl
| for ($file = "stdin", $line = 1; $_ = <stdin>;) {
| ($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(" ", at ARGV) . "|") ||
die "Cannot exec '$a' ($!)\n";
for ($file = "/dev/null", $line = 1; $_ = <CPP>;) {
($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 at intelob.intel.com> ...!uunet!tektronix!biin!merlyn |
\ Cute quote: "Welcome to Oregon... home of the California Raisins!" /
More information about the Comp.lang.c
mailing list