Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

s/// + tr/// when perl -ni -e on semi-colons in one liners echoed to files

by Don Coyote (Pilgrim)
on Nov 18, 2012 at 12:05 UTC ( #1004397=perlquestion: print w/ replies, xml ) Need Help??
Don Coyote has asked for the wisdom of the Perl Monks concerning the following question:

After rolling a FORMAT oneliner! :) I echo'd this into a file. Now my laziness is starting to niggle and I decide it is about time to edit in place those semicolons into newlines rather than doing this manually.

After clobbering the file a number of times. I could not get a 'simple' s/// or tr/// to operate on a perl -ni -e command. I eventually split the looping stdin file at the semi-colons and joined with newlines, and this was fine. But I wonder why I wasn't able to s/// or y/// on a looping file? some examples:

file to which one-liner echo'd

#! \usr\bin\perl use strict; use warnings; "my $fs = q!.\funkisym.pm!;open(FUNKISYM, q(>),qq($fs)); my $flashbang + = q{#! \usr\bin\perl};my $pra = q!use strict;!; my $pra2 = q!use war +nings;!;my @fstr = (q(format FUNKISYM = ),q(@).q(<) x 16,q($flashban +g).qq(\n),q(@).q(<) x 11,q($pra),q(@).q(<) x 13,q($pra2),qq(.\n));eva +l(join($/,@fstr));select FUNKISYM;write;exec(qq!notepad\.exe $fs!);"

attempted commands which either clobbered file and/or produced bareword deaths on tr///

perl -n -i.bak -e "tr/;/\n/;" .\funkisym.pm perl -n -i.bak -e "y/\Q;\E/\n/;" .\funkisym.pm perl -n -i.bak -e "s/(;)/$1\n/g;" .\funkisym.pm

I tried variations further to those shown, and settled for the following solution;

perl -ni.bak -e "my @text = split(q{;});print join(qq(;$/), @text);" . +\funkisym.pm"

Please can you suggest what I may be assuming incorrectly, in relation to this problem?

Comment on s/// + tr/// when perl -ni -e on semi-colons in one liners echoed to files
Select or Download Code
Re: s/// + tr/// when perl -ni -e on semi-colons in one liners echoed to files
by Anonymous Monk on Nov 18, 2012 at 12:31 UTC
Re: s/// + tr/// when perl -ni -e on semi-colons in one liners echoed to files
by Anonymous Monk on Nov 18, 2012 at 12:37 UTC

    Well, -n doesn't print, but -p does print :)

    $ perl -le " print qq{a;b;c;d} " a;b;c;d $ perl -le " print qq{a;b;c;d} " | perl -ne " tr/;/\n/ ; " $ perl -le " print qq{a;b;c;d} " | perl -pe " tr/;/\n/ ; " a b c d

      ok, thanks for the quick reply. It seems I was assuming -i edits 'in place'. And -n / -p only controlled if STDOUT was printed to or not.(i think it was SED).

      Just trying to analyse more complex stuff while tripping over the 'simple' stuff...

Re: s/// + tr/// when perl -ni -e on semi-colons in one liners echoed to files
by aitap (Deacon) on Nov 18, 2012 at 13:06 UTC
    You could feed this string to perltidy:
    $ perltidy my $fs = q!.\funkisym.pm!;open(FUNKISYM, q(>),qq($fs)); my $flashbang += q{#! \usr\bin\perl};my $pra = q!use strict;!; my $pra2 = q!use warn +ings;!;my @fstr = (q(format FUNKISYM = ),q(@).q(<) x 16,q($flashbang +).qq(\n),q(@).q(<) x 11,q($pra),q(@).q(<) x 13,q($pra2),qq(.\n));eval +(join($/,@fstr));select FUNKISYM;write;exec(qq!notepad\.exe $fs!); ^D my $fs = q!.\funkisym.pm!; open( FUNKISYM, q(>), qq($fs) ); my $flashbang = q{#! \usr\bin\perl}; my $pra = q!use strict;!; my $pra2 = q!use warnings;!; my @fstr = ( q(format FUNKISYM = ), q(@) . q(<) x 16, q($flashbang) . qq(\n), q(@) . q(<) x 11, q($pra), q(@) . q(<) x 13, q($pra2), qq(.\n) ); eval( join( $/, @fstr ) ); select FUNKISYM; write; exec(qq!notepad\.exe $fs!);
    (in this example ^D is used to end the input data; in Windows you'll need to use ^Z)
    Edit: correct input data and usage example
    Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004397]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2015-07-05 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls