Re: oneliner cat like capability
by choroba (Cardinal) on Oct 29, 2019 at 17:49 UTC
|
As documented in perlrun, -p already prints the input, so you can just use perl -pe "" dataoutput.txt
Or, if you want to print explicitly, use -n which loops over the input lines but doesn't print:
perl -ne 'print' dataoutput.txt
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] [select] |
|
perl -pe1 dataoutput.txt
:)
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
|
| [reply] |
|
... why is it important to have the 'n' or 'p' before the 'e'?
Per perlrun, the -e switch expects source text to follow, either with or without a space. The switch sequence -en means that Perl is trying to execute "n". The effect of this is made more clear with strictures enabled (see strict):
c:\@Work\Perl\monks>perl -wMstrict -en "print" foo get_bar()
Bareword "n" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.
c:\@Work\Perl\monks>perl -wMstrict -ep foo get_bar()
Bareword "p" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.
Update: See jwkrahn's example here of -pe1 which gives -e the source 1 (that's a digit 1) to evaluate within the source code framework established by the -p switch.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
It's not important. You can write the loop yourself:
perl -e 'print while <>' -- dataoutput.txt
It's just helpful: if you want to iterate over the input and print always, use -p; if you want to iterate but not print, use -n; otherwise, use neither.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] [select] |
Re: oneliner cat like capability
by Fletch (Bishop) on Oct 29, 2019 at 18:27 UTC
|
Another handy thing to know about is B::Deparse which will dump out the code as perl interprets it. If you use it with your oneliner you can see both print calls (from your argument to -e and the implicit one from -p in the generated continue block).
$ perl -MO=Deparse -pe 'print'
LINE: while (defined($_ = <ARGV>)) {
print $_;
}
continue {
die "-p destination: $!\n" unless print $_;
}
-e syntax OK
Depending on what you're trying to figure out there's options to B::Deparse which can be useful like -p to add explicit parens everywhere (-MO=Deparse,-p,-q is another handy variant).
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |
Re: oneliner cat like capability
by shmem (Chancellor) on Oct 29, 2019 at 17:51 UTC
|
However, it prints out each lines twice, which is in the same order as in the file. What am I doing wrong?
The -p switch implies print. See perlrun.
Invoking perl with -p and an empty script body does the trick:
perl -pe '' dataoutput.txt
In your second example, you are just printing a newline character before the implicit print is done, hence the empty lines.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] [select] |
Re: oneliner cat like capability
by stevieb (Canon) on Oct 29, 2019 at 20:57 UTC
|
"I am looking at doing and getting better with oneliners from the command line."
If that's the case, I would highly recommend adding -wMstrict to your one-liners, right after the perl command, like so: perl -wMstrict -insert_other_stuff_here.
It does appear through other responses that you're a beginner really trying to learn, so throwing the switches I've recommended into the mix while you're learning will provide you the benefit of learning both one liners, and the work involved in making full-blown Perl scripts and modules reliable and as trustworthy as possible. You want your code to operate flawlessly under use warnings (an expanded -w) and use strict (-Mstrict). Very few Perl coders trust code without these present.
I know that using one-liners are typically for one-off type things, but for a relative newcomer to the language, a few extra keystrokes across one-liners will get you into the proper mindset so when you want to use them for more advanced things, you'll be able to do so on the fly quickly, and reliably.
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] [d/l] [select] |
Re: oneliner cat like capability
by Anonymous Monk on Oct 30, 2019 at 07:45 UTC
|
Here is some really good homework:
https://metacpan.org/source/BDFOY/PerlPowerTools-1.016/bin/cat
When I type man which at osx it says:
HISTORY
The which command first appeared in FreeBSD 2.1.
AUTHORS
The which utility was originally written in Perl
Reinventing wheels is great fun and practice. You never
know "which" next great utility will be originally
written in Perl! :) Keep on Truckin' | [reply] |
Re: oneliner cat like capability
by Random_Walk (Prior) on Oct 31, 2019 at 23:03 UTC
|
Oneliner cat like capability ....
Am I the only one that thought meouw ....?
Cheers, R.
Pereant, qui ante nos nostra dixerunt!
| [reply] [d/l] |