Help!

Bash/Zsh-script: Reading a particular line from a file?


Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> General Discussions (archive 2) RSS
Next:  Command for joining two lines to one?  
Author Message
Koppe74
External


Since: Jan 20, 2005
Posts: 39



PostPosted: Thu Dec 21, 2006 9:30 am    Post subject: Bash/Zsh-script: Reading a particular line from a file?
Archived from groups: comp>os>linux>misc (more info?)

I've made several shell-scripts for bash and zsh, but I've never really
bothered to learn all the details. Whenever I've needed a paticular
line from a file (e.g. working on a file line-by-line), I've always
managed
to get by by piping together 'head' and 'tail' or using 'sed' (although
I'd think 'gawk' would also work). However, I was wondering if there
are some buildt-in for the shell -- or perhaps a option for cat -- that
would allow me to read a particular line of a file (e.g. something like
"my-file.txt{8]" to specify line 8 of the file "my-file").

-Koppe
Back to top
Dances With Crows
External


Since: Jun 07, 2006
Posts: 587



PostPosted: Thu Dec 21, 2006 12:15 pm    Post subject: Re: Bash/Zsh-script: Reading a particular line from a file? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 21 Dec 2006 09:30:54 -0800, Koppe74 staggered into the Black Sun and
said:
> Whenever I've needed a paticular line from a file I've always managed
> to get by by piping together 'head' and 'tail' or using 'sed' I was
> wondering if there are some built-ins for the shell -- or perhaps a
> option for cat -- that would allow me to read a particular line of a
> file (e.g. something like "my-file.txt{8]" to specify line 8 of the
> file "my-file").

Not shell, but:

#!/usr/bin/perl -w
open(IFP,"test.txt") or die "could not open test.txt: $!";
@lines=<IFP>;
# NOTE: If test.txt is a 930,000 line file, you'll eat a metric
# arseload of memory. Caveat programmer.
print "42nd line is ".$lines[41];
# end perl script

....you can probably do something similar in Python. A quick read
through the info pages says that you'd get line N from a file in a shell
programming context with head and tail, or awk, or sed. TMTOWTDI, pick
the one that works best. HTH,

--
One RAID to rule them all, One RAID to bind them
One RAID to hold the files and in the darkness grind them
In the land of Server where the Unix lies....
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Back to top
Frank Stutzman
External


Since: Apr 14, 2004
Posts: 7



PostPosted: Thu Dec 21, 2006 9:49 pm    Post subject: Re: Bash/Zsh-script: Reading a particular line from a file? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dances With Crows <danSPANceswitTRAPhcrows DeleteThis @gmail.com> wrote:
> Not shell, but:
>
> #!/usr/bin/perl -w
> open(IFP,"test.txt") or die "could not open test.txt: $!";
> @lines=<IFP>;
> # NOTE: If test.txt is a 930,000 line file, you'll eat a metric
> # arseload of memory. Caveat programmer.
> print "42nd line is ".$lines[41];
> # end perl script

Wuf! Load up an entire file into an array to get a single line?

Try:
perl -ne 'if (42..42) {print "42nd line is $_"; last;}' filename

note the use of the range operator there. The range is from 42 to 42.
One could easily generalize this so that they would see, say, lines
42 through 142, by changing it to:
perl -ne 'print $_ if (42..142);' filename

Note we lost some performance here to get the multiple lines. By
dropping the 'last;' statement, we are forced to read the entire file. At
least we arn't stuffing it into an array.

In the recesses of my mind there is also an elegant way of doing this in
awk, but I'm too lazy to try to remember it at the moment. I can't think
of a way at all to do this strictly with shell built-in fuctions, but I
could be wrong.


--
Frank Stutzman
Back to top
Dances With Crows
External


Since: Jun 07, 2006
Posts: 587



PostPosted: Thu Dec 21, 2006 9:49 pm    Post subject: Re: Bash/Zsh-script: Reading a particular line from a file? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 21 Dec 2006 21:49:22 +0000 (UTC), Frank Stutzman staggered into
the Black Sun and said:
> Dances With Crows <danSPANceswitTRAPhcrows RemoveThis @gmail.com> wrote:
>> The OP wrote:
>>> e.g. something like "my-file.txt{8]" to specify line 8 of the file
>>> "my-file".
>> open(IFP,"test.txt") or die "could not open test.txt: $!";
>> @lines=<IFP>;
>> # NOTE: If test.txt is a 930,000 line file, you'll eat a metric
>> # arseload of memory. Caveat programmer.
> Wuf! Load up an entire file into an array to get a single line?

This was not intended as a finished product, as the comment strongly
implies. If you know you want multiple lines, or you don't know which
lines you want until later, and the file is small enough, you can read
the whole file into an array. There are usually better ways to do
this--but it does satisfy the syntax the OP wanted.

> One could easily generalize this so that they would see, say, lines
> 42 through 142, by changing it to:
> perl -ne 'print $_ if (42..142);' filename

And if (1..1 5..5 11..11) if you wanted lines 1, 5, and 11.

> I can't think of a way at all to do this strictly with shell built-in
> fuctions, but I could be wrong.

count=1 # counting lines from 1
while read -r line ; do
if [[ $count == 2 || $count == 4 ]] ; then
echo $line
fi
let count=$count+1
if [[ $count > 4 ]] ; then break ; fi # no need to go beyond 4
done < filename

....prints lines 2 and 4 from filename, but it does read the whole thing
in. Just bash builtins, though you can probably do something very
similar for other shells.

--
Observe the haggis carefully for at least one minute. If you see any
movement, take out your revolver and shoot the haggis until it stops
moving. --Ground Control on everything2, "How to toast a Haggis".
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Back to top
Dances With Crows
External


Since: Jun 07, 2006
Posts: 587



PostPosted: Thu Dec 21, 2006 9:49 pm    Post subject: Re: Bash/Zsh-script: Reading a particular line from a file? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 21 Dec 2006 16:12:32 -0600, Dances With Crows staggered into the
Black Sun and said:
>> I can't think of a way at all to do this strictly with shell built-in
>> fuctions, but I could be wrong.
> count=1 # counting lines from 1
> while read -r line ; do
> if [[ $count == 2 || $count == 4 ]] ; then
[snip]
> if [[ $count > 4 ]] ; then break ; fi # no need to go beyond 4
> done < filename

@#$%^. The == should be -eq , and the > should be -gt , before the bash
gurus say "YOU SCREWED UP!!1!". Extracting line 5100 from a text file
with that change takes a couple of seconds with the bash loop. So
awk/perl might be faster for the OP if he has to do tons of this sort of
thing on large files.

--
So, what do *you* do for a living?
I sit in a chair, pressing small plastic rectangles with my fingers
while peering at many tiny, colored dots. --Peter Manders
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Back to top
Michael Mauch
External


Since: May 21, 2004
Posts: 45



PostPosted: Thu Dec 21, 2006 10:50 pm    Post subject: Re: Bash/Zsh-script: Reading a particular line from a file? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Frank Stutzman wrote:

> In the recesses of my mind there is also an elegant way of doing this in
> awk, but I'm too lazy to try to remember it at the moment.

awk 'FNR == 42' filename

> I can't think of a way at all to do this strictly with shell built-in
> fuctions, but I could be wrong.

You could use read and count the lines, but probably that's a bit slow
and ugly.

Michael
Back to top
martin.witte
External


Since: Apr 09, 2005
Posts: 15



PostPosted: Fri Dec 22, 2006 6:09 am    Post subject: Re: Bash/Zsh-script: Reading a particular line from a file? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Koppe74 wrote:
> I've made several shell-scripts for bash and zsh, but I've never really
> bothered to learn all the details. Whenever I've needed a paticular
> line from a file (e.g. working on a file line-by-line), I've always
> managed
> to get by by piping together 'head' and 'tail' or using 'sed' (although
> I'd think 'gawk' would also work). However, I was wondering if there
> are some buildt-in for the shell -- or perhaps a option for cat -- that
> would allow me to read a particular line of a file (e.g. something like
> "my-file.txt{8]" to specify line 8 of the file "my-file").
>
> -Koppe

You can do this with sed: "sed -n '8p' my-file" will print the eigth
line of file my-file
Back to top
Bill Marcum
External


Since: Dec 18, 2006
Posts: 286



PostPosted: Wed Jan 03, 2007 7:37 pm    Post subject: Re: Bash/Zsh-script: Reading a particular line from a file? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, 27 Dec 2006 12:52:37 +0000 (GMT), Anonymous
<nobody DeleteThis @mixmin.net> wrote:
>
>
>"FS" == Frank Stutzman <stutzman DeleteThis @skylane.kjsl.com>:
>FS> perl -ne 'if (42..42) {print "42nd line is $_"; last;}' filename
>FS>
>FS> In the recesses of my mind there is also an elegant way of doing this in
>FS> awk, but I'm too lazy to try to remember it at the moment.
>
> awk -n 42p filename
s/awk/sed/

awk 'NR==42' filename


--
Did you ever walk into a room and forget why you walked in? I think
that's how dogs spend their lives.
-- Sue Murphy
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> General Discussions (archive 2) All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum