IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

[Fichier] Supression saut ligne


Sujet :

Langage Perl

  1. #1
    Nouveau membre du Club
    Inscrit en
    Mai 2007
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 39
    Points : 36
    Points
    36
    Par défaut [Fichier] Supression saut ligne
    Bonjour,

    Je rencontre un problème avec un fichier que j'aimerais modifier je suis certain que l'on peux passer par un script perl pour ceci.

    J'ai fait ce script shell qui donne une idee de ce que je voudrais obtenir
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    NBRL=5
    # Supression des retour à la ligne
    while test $NBRL != 1
    do
    sed -i 'N;s/\n//g' teste 
    NBRL=`more teste | wc -l`
    echo $NBRL
    done
    # création des retour a la ligne ou j'en ai besoin
    sed -i "s/<ListeClient>/<ListeClient>\n/g" teste
    sed -i "s/<\/Client>/<\/Client>\n/g" teste
    ce script fonctionne bien sous mon linux mais sed -i sous AIX ne fonctionne pas movaise version de sed.

    en gros j'ai un fichier qui ressemble a ca
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     
    <ListeClient>
      <Client etatDossier="10">
        <AttributClient nomAttribut=""></AttributClient>
        <AttributClient nomAttribut=""></AttributClient>
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
      </Client>
      <Client etatDossier="10">
        <AttributClient nomAttribut=""></AttributClient>
        <AttributClient nomAttribut=""></AttributClient>
        <AttributClient nomAttribut=""></AttributClient>
        <AttributClient nomAttribut=""></AttributClient>
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
        <ServicePrest code="" />
      </Client>
    </ListeClient>
    et j'aimerais obtenir
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    <ListeClient>
      <Client etatDossier="10">
    <AttributClient nomAttribut=""></AttributClient>    <AttributClient nomAttribut="dossierClient"></AttributClient>    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />
      </Client>
      <Client etatDossier="10">
        <AttributClient nomAttribut=""></AttributClient>    <AttributClient nomAttribut="dossierClient"></AttributClient>    <AttributClient nomAttribut="OriginInfo"></AttributClient>    <AttributClient nomAttribut="segCo"></AttributClient>    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />    <ServicePrest code="" />
      </Client>
    </ListeClient>
    Si quelqu'un pouvais m'orienter afin de savoir comment transformer un fichier via perl et sed merci d'avance

  2. #2
    Membre éprouvé Avatar de Gardyen
    Homme Profil pro
    Bio informaticien
    Inscrit en
    Août 2005
    Messages
    637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bio informaticien

    Informations forums :
    Inscription : Août 2005
    Messages : 637
    Points : 1 050
    Points
    1 050
    Par défaut
    voila un petit exemple de comment modifier un fichier avec perl

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    open (IN, "D:/test.txt");
    open (OUT, ">D:/test2.txt");
    while (<IN>){
    	chomp;
    	if (/<\/?Client/ || /<ListeClient>/){
    		$_ .= "\n";
    	}
    	print OUT $_;
     
    }
    close(IN);
    close(OUT);
    le fichier test.txt est :
    <ListeClient>
    <Client etatDossier="10">
    <AttributClient nomAttribut=""></AttributClient>
    <AttributClient nomAttribut=""></AttributClient>
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    </Client>
    <Client etatDossier="10">
    <AttributClient nomAttribut=""></AttributClient>
    <AttributClient nomAttribut=""></AttributClient>
    <AttributClient nomAttribut=""></AttributClient>
    <AttributClient nomAttribut=""></AttributClient>
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    <ServicePrest code="" />
    </Client>
    </ListeClient>
    le fichier obtenu est
    <ListeClient>
    <Client etatDossier="10">
    <AttributClient nomAttribut=""></AttributClient> <AttributClient nomAttribut=""></AttributClient> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> </Client>
    <Client etatDossier="10">
    <AttributClient nomAttribut=""></AttributClient> <AttributClient nomAttribut=""></AttributClient> <AttributClient nomAttribut=""></AttributClient> <AttributClient nomAttribut=""></AttributClient> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> <ServicePrest code="" /> </Client>
    </ListeClient>
    ensuite il te reste plus qu'a renommer les fichiers...
    Nous les geeks, c'est pas qu'on a une case en moins, c'est juste qu'on compte à partir de zéro.
    Plus les choses changent, plus elles restent les mêmes

  3. #3
    Expert éminent
    Avatar de Jedai
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2003
    Messages
    6 245
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2003
    Messages : 6 245
    Points : 8 586
    Points
    8 586
    Par défaut
    Tu peux remplacer ton script bash par une ligne de Perl :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    perl -i -pe 'chomp unless m{</Client>} or m{<ListeClient>}' teste
    --
    Jedaï

  4. #4
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 498 771
    Points
    498 771
    Par défaut
    vive perl

Discussions similaires

  1. fichier et saut de ligne
    Par alexlechevalier dans le forum C#
    Réponses: 6
    Dernier message: 14/11/2008, 13h58
  2. supression des lignes d'un fichier texte
    Par anna1980 dans le forum Général Python
    Réponses: 3
    Dernier message: 09/06/2008, 11h34
  3. [MySQL] Import d'un fichier sans saut de ligne
    Par Pgs dans le forum PHP & Base de données
    Réponses: 12
    Dernier message: 09/05/2008, 14h07
  4. Réponses: 12
    Dernier message: 02/01/2008, 13h16
  5. [Tomcat Bean IO] Ecriture sur Fichier et Saut de Ligne
    Par zaoueche dans le forum Tomcat et TomEE
    Réponses: 2
    Dernier message: 25/01/2006, 15h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo