|
|
|
|
Hello !
Grâce à vous tous et aux tutoriels sur internet, j'ai réussi à faire tourner sous Unix mes premiers programmes perl.
Il y a cependant quelque chose que je ne parviens pas à faire:
Comment on extrait des données d'un fichier texte ?
Je m'explique:
Voici par exemple mon fichier texte fichier1.txt :
okokokok x y t w a
aaaaa 1 2 3 4 5
45678 2 9 8 4 3
J'aimerais creer À partir de ce fichier un autre fichier texte fichier2.txt qui contiendrait juste les colonnes t et a:
nouveau fichier texte fichier2.txt:
# t a
3 5
8 3
Quelqu'un sait faire ca ? Ou me mettre sur la voie ?
merci.
Miguel
Bonjour,
#!/usr/bin/perl -w
use strict;
use warnings;
open(FILE, "fichier1.txt") || die "Erreur E/S : $!\n";
my @contenu = <FILE>;
close(FILE);
open(FILE, "fichier2.txt") || die "Erreur E/S : $!\n";
foreach (@contenu) {
my @datas = split(/ /, $_);
print FILE "$datas[3] $datas[5]\n";
}
close(FILE);
Philippe --- O Espirito da Liberdade --- |
Merci Philippe !
|
La fonction split "éclate" la chaine stockée dans la variable $_ en fonction du délimiteur se trouvant entre les /, donc ici le caractère blanc.
|
Salut,
[jp@MDK tmpfs]$ cat file.txt
okokokok x y t w a
101 1 2 3 4 5
102 2 9 8 4 3
103 5 4 8 7 3
104 5 9 8 7 6
105 2 5 7 8 9
[jp@MDK tmpfs]$ egrep "102|105" file.txt | awk '{ print $4" " $6 }'
8 3
7 9
Ou dans un script :
[jp@MDK tmpfs]$ cat batch.sh
#! /bin/bash
egrep "$1|$2" < file.txt | awk '{ print $4" "$6 }'
[jp@MDK tmpfs]$ sh batch.sh 102 105
8 3
7 9
[jp@MDK tmpfs]$;-))
Z'@+...che.JP : Zen, my Nuggets ! ;-) Le savoir n'est bon que s'il est partagé. |
Salut,
[lamitest@localhost corbeille]$ cat ccm_file_fifto okokokok x y t w a aaaaa 1 2 3 4 5 45678 2 9 8 4 3 [lamitest@localhost corbeille]$ perl -pi.orig -e 's/[\w\s]+(\w\s)\w\s(\w\s)/$1$2/g' ccm_file_fifto [lamitest@localhost corbeille]$ cat ccm_file_fifto t a 3 5 8 3 [lamitest@localhost corbeille]$ |
Re,
#! /usr/bin/perl
use warnings;
@ARGV = qw (/home/lami/fifto.txt);
open FIC_W,">","/home/lami/fifto_res.txt"
or die "Impossible de creer le fichier : $!_n";
while (<>){
s/(.*)/join " ",(split " ", $1)[3,5]/e;
print FIC_W $_;
}
Le résultat [root@localhost ~]# cat /home/lami/fifto.txt okokok x y t w a aaaaa 1 2 3 4 5 45678 2 9 8 4 3 [root@localhost ~]# ls /home/lami/fifto_res.txt ls: /home/lami/fifto_res.txt: Aucun fichier ou répertoire de ce type [root@localhost ~]# perl ccm_fifto2 [root@localhost ~]# cat /home/lami/fifto_res.txt t a 3 5 8 3 [root@localhost ~]# |
Salut les experts,
|