PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > display multiple lines from file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

display multiple lines from file

Réponse
 
LinkBack Outils de la discussion
Vieux 08/05/2008, 11h17   #1
Nezhate
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 me to use a for loop to echo all lines that
I want to print.
cheers
  Réponse avec citation
Vieux 08/05/2008, 11h25   #2
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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.
  Réponse avec citation
Vieux 08/05/2008, 11h45   #3
Dave B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 08/05/2008, 12h27   #4
Janis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

> knowing this it will me to use a for loop to echo all lines that
> I want to print.
> cheers


  Réponse avec citation
Vieux 09/05/2008, 03h13   #5
mo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: display multiple lines from file

On Thu, 08 May 2008 07:17:45 -0300, Nezhate <mazouz.nezhate@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 ?
> 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 me to use a for loop to echo all lines that
> I want to print.
> cheers


#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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h11.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11569 seconds with 13 queries