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.
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
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.
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."
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