Google Groups Home
Help | Sign in
display multiple lines from file
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Nezhate  
View profile
(1 user)  More options May 8, 6:17 am
Newsgroups: comp.unix.shell
From: Nezhate <mazouz.nezh...@gmail.com>
Date: Thu, 8 May 2008 03:17:45 -0700 (PDT)
Local: Thurs, May 8 2008 6:17 am
Subject: display multiple lines from file
Hi all,
I'm looking for command that print any number of lines from a given
file.
For example file1.txt contains 100 lines
using the head command I can printout the first n lines from
file1.txt.
using the tail command I can printout the n last lines from file1.txt
How to do for printing out a block of lines, let say from the 50 th
line to 68 th line ?
I used this but it fails

file_name="file1.txt"

if [ -f  $file_name ] ;then
  tail +$1 $3 | head -n$2
fi

Another question:
How to detect the total number of lines from a given file?
knowing this it will help me to use a for loop to echo all lines that
I want to print.
cheers


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave B  
View profile
(1 user)  More options May 8, 6:25 am
Newsgroups: comp.unix.shell
From: Dave B <da...@addr.invalid>
Date: Thu, 08 May 2008 12:25:42 +0200
Local: Thurs, May 8 2008 6:25 am
Subject: Re: display multiple lines from file
On Thursday 8 May 2008 12:17, Nezhate wrote:

> Hi all,
> I'm looking for command that print any number of lines from a given
> file.
> For example file1.txt contains 100 lines
> using the head command I can printout the first n lines from
> file1.txt.
> using the tail command I can printout the n last lines from file1.txt
> How to do for printing out a block of lines, let say from the 50 th
> line to 68 th line ?

sed -n '50,68p' yourfile

> Another question:
> How to detect the total number of lines from a given file?
> knowing this it will help me to use a for loop to echo all lines that
> I want to print.

What you want can be done using other (more efficient) means, however to
know the number of lines in a file there are many ways, for instance:

sed -n '$=' yourfile
awk 'END {print NR}' yourfile
wc -l < yourfile

--
D.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave B  
View profile
(1 user)  More options May 8, 6:45 am
Newsgroups: comp.unix.shell
From: Dave B <da...@addr.invalid>
Date: Thu, 08 May 2008 12:45:41 +0200
Local: Thurs, May 8 2008 6:45 am
Subject: Re: display multiple lines from file
On Thursday 8 May 2008 12:25, Dave B wrote:

>> How to do for printing out a block of lines, let say from the 50 th
>> line to 68 th line ?

> sed -n '50,68p' yourfile

If the file is very large, you probably don't want sed to read the rest of
the file, so do

sed -n '50,68p;68q' yourfile

--
D.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Janis  
View profile
(1 user)  More options May 8, 7:27 am
Newsgroups: comp.unix.shell
From: Janis <janis_papanag...@hotmail.com>
Date: Thu, 8 May 2008 04:27:07 -0700 (PDT)
Local: Thurs, May 8 2008 7:27 am
Subject: Re: display multiple lines from file
On 8 Mai, 12:17, Nezhate <mazouz.nezh...@gmail.com> wrote:

> Hi all,
> I'm looking for command that print any number of lines from a given
> file.
> For example file1.txt contains 100 lines
> using the head command I can printout the first n lines from
> file1.txt.
> using the tail command I can printout the n last lines from file1.txt
> How to do for printing out a block of lines, let say from the 50 th
> line to 68 th line ?

awk 'NR>=50&&NR<=68' yourfile

awk 'NR==50,NR==68' yourfile

> I used this but it fails

In what way does it fail?

> file_name="file1.txt"

> if [ -f  $file_name ] ;then
>   tail +$1 $3 | head -n$2

Are you sure you intended to use $3 instead of $filename?

> fi

> Another question:
> How to detect the total number of lines from a given file?

  wc -l <yourfile

Or if you want to combine that information with the other
command above and use just a single (awk) program

  awk 'NR==50,NR==68; END{print NR}' yourfile

Janis


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mo  
View profile
 More options May 8, 10:13 pm
Newsgroups: comp.unix.shell
From: mo <inva...@mail.address>
Date: Thu, 08 May 2008 23:13:10 -0300
Local: Thurs, May 8 2008 10:13 pm
Subject: Re: display multiple lines from file
On Thu, 08 May 2008 07:17:45 -0300, Nezhate <mazouz.nezh...@gmail.com>  
wrote:

#Q1
file_name="file1.txt"

if [ -f  $file_name ] ;then
  head -n$2 $file_name|tail -n$(($2-$1+1))
fi

#Q2
wc -l $file_name


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google