Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!bbn!oliveb!mipos3!omepd!merlyn From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.unix.wizards Subject: xargs in Perl (was Re: Look! An xargs!! (Re: recursive grep)) Message-ID: <4916@omepd.UUCP> Date: 9 Sep 89 05:17:08 GMT References: <1122@virtech.UUCP> <680@lakart.UUCP> Sender: news@omepd.UUCP Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Hillsboro, Oregon, USA Lines: 41 In-reply-to: dg@lakart.UUCP (David Goodenough) In article <680@lakart.UUCP>, dg@lakart (David Goodenough) writes: | OK - will you all get off my case, already!! [awk program deleted] And, here it is in Perl (of course...)... ================================================== cut here #!/usr/bin/perl $lengthmax = 512; @command = $#ARGV > -1 ? @ARGV : ("/bin/echo"); $lengthcommand = length(join(" ", @command)) + 1; while () { if ($lengthcommand + length($args) + length($_) > $lengthmax) { system @command, split(/\n/, $args); $err = 1 if $?; $args = ""; } $args .= $_; } if (length($args)) { system @command, split(/\n/, $args); $err = 1 if $?; } exit $err; ================================================== cut here too This version handles commands with *any* special characters (provided you had already quoted it for the shell properly), and arguments with any characters *except* newline (gotta have *one* reserved character :-). | So there :-P Like I said, I do it all with awk :-) And, I do it all with Perl, now. Just another Perl hacker, -- /== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\ | on contract to Intel, Hillsboro, Oregon, USA | | merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn | \== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/