f



how to read a line from a file on command line

How to read  a specific line from a file on command line ?
For instance, I have  file named test.txt, and I like to read the line
45,  one single line only.
Does any command I can retrieve the line 45 from the file text.txt ?
Thanks.
0
moongeegee (52)
7/18/2008 5:25:54 PM
comp.os.linux.misc 33599 articles. 1 followers. amosa69 (78) is leader. Post Follow

4 Replies
574 Views

Similar Articles

[PageSpeed] 43

On Friday 18 July 2008 19:25, moongeegee wrote:

> How to read  a specific line from a file on command line ?
> For instance, I have  file named test.txt, and I like to read the line
> 45,  one single line only.
> Does any command I can retrieve the line 45 from the file text.txt ?
> Thanks.

sed -n '45{p;q}' text.txt

0
pk (435)
7/18/2008 5:31:56 PM
On 2008-07-18, moongeegee <moongeegee@gmail.com> wrote:

> How to read a specific line from a file on command line ? For
> instance, I have file named test.txt, and I like to read the
> line 45, one single line only. Does any command I can retrieve
> the line 45 from the file text.txt ? Thanks.

awk NR==45 text.txt

tail -n+45 text.txt | head -n1

python -c 'print open("text.txt","r").readlines()[44],'

[perl and sed implementations left as an excersize for the reader.]


-- 
Grant Edwards                   grante             Yow! Are we laid back yet?
                                  at               
                               visi.com            
0
grante (5416)
7/18/2008 7:00:44 PM
On Friday 18 July 2008 21:00, Grant Edwards wrote:

> On 2008-07-18, moongeegee <moongeegee@gmail.com> wrote:
> 
>> How to read a specific line from a file on command line ? For
>> instance, I have file named test.txt, and I like to read the
>> line 45, one single line only. Does any command I can retrieve
>> the line 45 from the file text.txt ? Thanks.
> 
> awk NR==45 text.txt

awk 'NR==45{print;exit}' text.txt

is faster with large files (and probably a good idea anyway).

> tail -n+45 text.txt | head -n1

head -n 45 text.txt | tail -n 1

is probably more efficient (although I'm not sure).

0
pk (435)
7/18/2008 9:36:14 PM
On 2008-07-18, moongeegee wrote:
> How to read  a specific line from a file on command line ?
> For instance, I have  file named test.txt, and I like to read the line
> 45,  one single line only.
> Does any command I can retrieve the line 45 from the file text.txt ?

    If there is only one line in the file, you don't need any external
    command:

read var < test.txt

    If you want to read two or three lines from a file (there are
    better ways to do it if you need more than a few lines):

{
 read line1
 read line2
 read line3
} < text.txt

-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |         is released under the
   2005, Apress                 |    GNU General Public Licence
0
cfajohnson (1827)
7/18/2008 10:58:29 PM
Reply: