Re: perl one liner to print a line
by jwkrahn (Abbot) on Dec 12, 2008 at 01:43 UTC
|
perl -ne'print if $. == 4' file.txt
| [reply] [d/l] |
|
|
perl -ne'print, last if $. == 4'
| [reply] [d/l] |
|
|
perl -ne'print and close ARGV if $. == 4' file.txt
It works better with multiple files on the command line. ;-)
| [reply] [d/l] [select] |
Re: perl one liner to print a line
by ikegami (Patriarch) on Dec 12, 2008 at 01:58 UTC
|
The array approach would be
perl -e'@x=<>; print $x[3]'
but that loads the entire file into memory where the $. method doesn't. Just putting it out there since you tried to use an array.
| [reply] [d/l] [select] |
|
|
yea... i dont want to take the whole file into memory. so $. is a better way for me :)
| [reply] |
|
|
perl -F'\n' -lan0777e'print $F[3]' file.txt
| [reply] [d/l] |
Re: perl one liner to print a line
by oko1 (Deacon) on Dec 12, 2008 at 02:37 UTC
|
perl -wne'print if 4..4' file.txt
--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf
| [reply] [d/l] |
Re: perl one liner to print a line
by kyle (Abbot) on Dec 12, 2008 at 02:28 UTC
|
perl -e '<> for 1..3; print scalar <>' file.txt
| [reply] [d/l] |
Re: perl one liner to print a line
by Lawliet (Curate) on Dec 12, 2008 at 02:55 UTC
|
| [reply] [d/l] |
|
|
| [reply] |
Re: perl one liner to print a line
by mr_mischief (Monsignor) on Dec 12, 2008 at 04:08 UTC
|
YAWTDI, this time with no whitespace in the source:
perl -e '{$_=<>;$.<4?redo:print}'
That really does look kinda like line noise. I wouldn't recommend that for serious work, of course. | [reply] [d/l] |
|
|
perl -ne'print,last,if$.==4'
12345678901234567890123 23
Update: 5 chars shorter:
perl -nlE'say,last,if$.==4'
1234567890123456789012 22
| [reply] [d/l] [select] |
|
|
perl -ne'4..4&&print'
| [reply] [d/l] |
|
|
|
|
I can save another character if we reintroduce whitespace:
perl -ne'$.-4or print,last'
or almost identically:
perl -ne'$.<4or print,last'
Update:
perl -nlE'$.<4or say,last'
123456789 123456789 1 21
| [reply] [d/l] [select] |
Re: perl one liner to print a line
by telemachus (Friar) on Dec 12, 2008 at 13:14 UTC
|
Lots of people have suggested good ways to do it, but here are a few points about your original version. perl -i -e 'while(<>){print $_[3];}' file.txt
The -i switch says that you want to edit in place. Normally you use that like this: -i.bak. The .bak (or whatever else) becomes a suffix for a copy of your original file. The original becomes file.txt.bak and file.txt gets edited. If you use -i by itself without a suffix, then you don't get a backup. That's dangerous. Since you're not editing at all here, you don't want the switch. (In this example, if you had successfully done print if $. == 4 and used the -i switch with no suffix, you would get no backup and only line 4 left in your original file. I don't think that's what you wanted.) The -p and -n switches are worth looking at in perlrun since they will give you the while(<>) loop for free. | [reply] [d/l] |
|
|
How about if I want to print the first line that match a regrex expression.
Thanks,
ngungo (newbie)
| [reply] |
|
|
Hi, new questions go in Seekers Of Perl Wisdom because
telemachus, whom you asked a question, hasn't been here in 10 weeks.
The node to which you replied is 2+ years old.
Welcome, see The Perl Monks Guide to the Monastery, see How do I post a question effectively?, Where should I post X?
See perlintro, Tutorials: Pattern Matching, Regular Expressions, and Parsing, perlretut, FMTYEWTK About Mass Edits In Perl, Uncommon* but Useful Perl Command Line Options for One-liners, Re^2: perl -pi -e s'/^\s+//'g $file
| [reply] |
Re: perl one liner to print a line (GOLF!)
by ccn (Vicar) on Dec 12, 2008 at 07:14 UTC
|
perl -lne 'die"$_\n"if$.==4'
perl -pe '$_=($.-4)?"":$_'
| [reply] [d/l] [select] |
|
|
die"$_\n" sends to STDERR, so you need to add " 2>&1".
The second doesn't do the same thing as the other golfs so far. It reads the entire file whereas the other golfs spent extra character to stop after 4 lines. If reading the entire file is ok, then I put forth
perl -lnE"$.^4||say"
123456789012345 15
| [reply] [d/l] [select] |