Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

slurp from commandline?

5 views
Skip to first unread message

Alan_C

unread,
Jun 6, 2006, 3:53:28 AM6/6/06
to begi...@perl.org
Hi,

perl -pi.bak -e 'tr/\xA0/ /' filenames*

# all files in a folder
find . | xargs perl -p -i.bak -e 's/oldstring/newstring/g'

perl -e 's/string/stringier/gi' -p -i.bak *.html

I'm accustomed to some of those. But how do I or is it possible to file slurp
on the command line, substituting \n+ with \n

IOW the next code does it. But can I do this slurp/substitute directly on the
commandline? How?

#!/usr/bin/perl -w
use strict;

my $old = shift;
my $new = "$old.tmp";
open(OLD, "<", $old) or die "cant open $old: $!";
my $text = do { local $/; <OLD> };
open(NEW, ">", $new) or die "cant open $new: $!";
$text =~ s/\n+/\n/g;
print NEW <<HAL;
$text
HAL
close(OLD) or die "cant close $old: $!";
close(NEW) or die "cant close $new: $!";
rename($old, "$old.orig") or die "cant rename $old to $old.orig: $!";
rename($new, "$old") or die "cant rename $new to $old: $!";

--
Alan.

John W. Krahn

unread,
Jun 6, 2006, 4:13:35 AM6/6/06
to Perl Beginners
Alan_C wrote:
> Hi,

Hello,

> perl -pi.bak -e 'tr/\xA0/ /' filenames*
>
> # all files in a folder
> find . | xargs perl -p -i.bak -e 's/oldstring/newstring/g'
>
> perl -e 's/string/stringier/gi' -p -i.bak *.html
>
> I'm accustomed to some of those. But how do I or is it possible to file slurp
> on the command line, substituting \n+ with \n

It is explained in the perlrun document for the -0 (zero) switch.

perldoc perlrun

John
--
use Perl;
program
fulfillment

Alan_C

unread,
Jun 6, 2006, 5:42:06 AM6/6/06
to begi...@perl.org
On Tuesday 06 June 2006 01:13, John W. Krahn wrote:
[ few example commands ]

> > I'm accustomed to some of those. But how do I or is it possible to file
> > slurp on the command line, substituting \n+ with \n
>
> It is explained in the perlrun document for the -0 (zero) switch.
>
> perldoc perlrun

Too cryptic -- IOW I'm not "high enough expertise in the command line
department" to be able to understand/grasp from (any) of that. (I tried) I
need simpler example, explanation -- that one be too high and too busy (for
me, now).

Next I looked perlfaq6

How can I pull out lines between two patterns that are themselves on different
lines?

[ snip ]

If you wanted text and not lines, you would use

perl -0777 -ne 'print "$1\n" while /START(.*?)END/gs' file1 file2 ...

perl -0777 -ne 's/\n+/\n/g' rsync_sl_log.txt

^^ my 1st attempt, didn't work

perl -0777 -ne 's/\n+/\n/g' while <> rsync_sl_log.txt

^^ 2nd attempt, didn't work ^^

perl -0777 -pne 's/\n+/\n/g' rsync_sl_log.txt

^^ 3rd -- aha!!! prints to screen with extra \n's removed!!!!!!!

perl -0777 -pne 's/\n+/\n/g' rsync_sl_log.txt > rsync_sl_log.txt.new

^^ 4th attempt. Bingo!!!! Works!!!!!

Though I'm unsure if I'm attempting to mix shell and Perl there (bash shell
redirection operator: > redirect STDOUT to a file)

Though it works, does anyone have any further refinement ideas?

Thanks.

--
Alan.

Muma W.

unread,
Jun 6, 2006, 6:02:33 AM6/6/06
to Beginners List
Alan_C wrote:
> [...]

> perl -0777 -pne 's/\n+/\n/g' rsync_sl_log.txt > rsync_sl_log.txt.new
>
> ^^ 4th attempt. Bingo!!!! Works!!!!!
>
> Though I'm unsure if I'm attempting to mix shell and Perl there (bash shell
> redirection operator: > redirect STDOUT to a file)
>
> Though it works, does anyone have any further refinement ideas?
>
> Thanks.
>

It sounds like you're trying to remove blank lines. This should also do
that:

perl -ne '/./ && print' rsync_sl_log.txt

It says "if there is at least a single character on the line, print the
line."

John W. Krahn

unread,
Jun 6, 2006, 7:04:41 AM6/6/06
to Perl Beginners
Alan_C wrote:
> On Tuesday 06 June 2006 01:13, John W. Krahn wrote:
> [ few example commands ]
>>>I'm accustomed to some of those. But how do I or is it possible to file
>>>slurp on the command line, substituting \n+ with \n
>>It is explained in the perlrun document for the -0 (zero) switch.
>>
>>perldoc perlrun
>
> Too cryptic -- IOW I'm not "high enough expertise in the command line
> department" to be able to understand/grasp from (any) of that. (I tried) I
> need simpler example, explanation -- that one be too high and too busy (for
> me, now).
>
> Next I looked perlfaq6
>
> How can I pull out lines between two patterns that are themselves on different
> lines?
>
> [ snip ]
>
> If you wanted text and not lines, you would use
>
> perl -0777 -ne 'print "$1\n" while /START(.*?)END/gs' file1 file2 ...
>
> perl -0777 -ne 's/\n+/\n/g' rsync_sl_log.txt
>
> ^^ my 1st attempt, didn't work

That is because you are using the -n switch which doesn't print by default.
You need to use the -p switch instead.


> perl -0777 -ne 's/\n+/\n/g' while <> rsync_sl_log.txt
>
> ^^ 2nd attempt, didn't work ^^

Again, you need to use the -p switch instead of the -n switch.


> perl -0777 -pne 's/\n+/\n/g' rsync_sl_log.txt
>
> ^^ 3rd -- aha!!! prints to screen with extra \n's removed!!!!!!!
>
> perl -0777 -pne 's/\n+/\n/g' rsync_sl_log.txt > rsync_sl_log.txt.new
>
> ^^ 4th attempt. Bingo!!!! Works!!!!!

You need to use EITHER the -p switch OR the -n switch but not both.


> Though I'm unsure if I'm attempting to mix shell and Perl there (bash shell
> redirection operator: > redirect STDOUT to a file)
>
> Though it works, does anyone have any further refinement ideas?

perl -i.old -p0777e'y/\n//s' rsync_sl_log.txt

perl -li.old -p00e1 rsync_sl_log.txt

0 new messages