How can I extract the whole line in a row, for example, row 3. These data are saved in my text editor in linux. Here's my data:
1,julz,kath,shiela,angel
2,may,ann,janice,aika
3,christal,justine,kim
4,kris,allan,jc,mine
I want output like:
3,christal,justine,kim
-
You can assign the diamond operator on your filehandle to a list, each element will be a line or row.
open $fh, "myfile.txt"; my @lines = <$fh>;
EDIT: This solution grabs all the lines so that you can access any one you want, e.g. row 3 would be
$lines[2]
... If you really only want one specific line, that'd be a different solution, like the other answerers'.dland : If the file is huge (that is, larger than RAM), this would be a very bad solution since it slurps the entire contents into memory.Adam Bellaire : Yes, that's true, this solution would only be good for small files, like the one given in the example.Aristotle Pagaltzis : Computers today have a lot of memory. If this code is going to run often or on multiple-hundred-megabyte files, then it’s worth worrying about memory, but if it runs just once in a while on files smaller than that, then it’s more important to keep the code as trivial as possible.From Adam Bellaire -
$ perl -ne'print if $. == 3' your_file.txt
Below is a script version of @ysth's answer:
$ perl -mTie::File -e'tie @lines, q(Tie::File), q(your_file.txt); > print $lines[2]'
From J.F. Sebastian -
The following snippet reads in the first three lines, prints only the third then exits to ensure that no unnecessary processing takes place.
Without the exit, the script would continue to process the input file despite you knowing that you have no use for it.
perl -ne 'if ($. == 3) {print;exit}' infile.txt
As perlvar points out,
$.
is the current line number for the last file handle accessed.From paxdiablo -
If it's always the third line:
perl -ne 'print if 3..3' <infile >outfile
If it's always the one that has a numeric value of "3" as the first column:
perl -F, -nae 'print if $F[0] == 3' <infile >outfile # thanks for the comment doh!
Since you didn't say how you were identifying that line, I am providing alternatives.
From Randal Schwartz -
Um, the -n answers are assuming the question is "what is a script that...". In which case, perl isn't even the best answer. But I don't read that into the question.
In general, if the lines are not of fixed length, you have to read through a file line by line until you get to the line you want. Tie::File automates this process for you (though since the code it would replace is so trivial, I rarely bother with it, myself).
use Tie::File; use Fcntl "O_RDONLY"; tie my @line, "Tie::File", "yourfilename", mode => O_RDONLY or die "Couldn't open file: $!"; print "The third line is ", $line[2];
paxdiablo : You lost me in the first paragraph - the question is tagged perl so what would we use other than a perl script? Not being nitpicky, it's just that enquiring minds want to know. :-)ysth : A snippet of perl code. There are few programming questions for which the answer is a complete script.From ysth -
For a more general solution:
open my $fh, '<', 'infile.txt'; while (my $line = <$fh>) { print $line if i_want_this_line($line); }
where i_want_this_line implements the criteria defining which line(s) you want.
From Dave Sherohman
0 comments:
Post a Comment