Afficher une ligne particulière d'un fichier

Résolu/Fermé
moun - 21 avril 2005 à 17:27
 andok - 17 juil. 2017 à 11:13
bonjour

je voudrais savoir comment extraire ou afficher une ligne particulière d'un fichier en shell (par exemple : la ligne 3 du fichier toto) pour en faire une variable

merci

6 réponses

jipicy Messages postés 40842 Date d'inscription jeudi 28 août 2003 Statut Modérateur Dernière intervention 10 août 2020 4 895
21 avril 2005 à 21:56
Salut,

Tu peux faire ça avec "sed" aussi:
soit le fichier "fich"
[jp@Mandrake scripts]$ cat fich

nom_fichier_1 ; commentaire associé au fichier 1
nom_fichier_2 ; commentaire associé au fichier 2
nom_fichier_4 ; commentaire associé au fichier 4
nom_fichier_5 ; commentaire associé au fichier 5
nom_fichier_7 ; commentaire associé au fichier 7
nom_fichier_9 ; commentaire associé au fichier 9
nom_fichier_10 ; commentaire associé au fichier 10
nom_fichier_12 ; commentaire associé au fichier 12
nom_fichier_14 ; commentaire associé au fichier 14
nom_fichier_15 ; commentaire associé au fichier 15
nom_fichier_16 ; commentaire associé au fichier 16

Pour extraire la 3 ème ligne
[jp@Mandrake scripts]$ sed -n 3p fich
nom_fichier_4 ; commentaire associé au fichier 4

La 5 ème :
[jp@Mandrake scripts]$ sed -n 5p fich
nom_fichier_7 ; commentaire associé au fichier 7

Pour la récupérer dans une variable :
[jp@Mandrake scripts]$ var=`sed -n 5p fich`
[jp@Mandrake scripts]$ echo $var
nom_fichier_7 ; commentaire associé au fichier 7
[jp@Mandrake scripts]$
;-))
31
Merci beaucoup mon cher.
0