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

Linux Discussion :

Remplacement de date dans un fichier texte


Sujet :

Linux

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2007
    Messages : 3
    Points : 1
    Points
    1
    Par défaut Remplacement de date dans un fichier texte
    Bonjour,

    Je suis totalement novice dans la programmation, mais j'aurai besoin d'un script pouvant modifier un fichier texte.

    mon fichier texte comprend ceci :

    "06292007","0000","C1","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","0030","C1","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","0100","C1","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","0130","C1","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","0200","C1","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","0230","C1","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","0300","C1","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    ....
    "06292007","2100","C60","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","2130","C60","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","2200","C60","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","2230","C60","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","2300","C60","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""
    "06292007","2330","C60","","CAB","0030","","","","","","","","","","","","","","","","","","","","","","","","","",""

    donc 06292007 qui corréspond à la date du jour 29/06/2007
    je aurai aimé trouvé/faire un script me permettant de modifier cette valeur avec la valeur du jour suivant en l'occurence 30/06/2007 soit 06302007

    Ensuite j'aurai un fichier comprenant 7 jours et donc il faudrait générer un fichier toutes les semaines avec les dates corrépsondantes.

    Merci de m'aiguiller vers la bonne voie.

    Serge.

  2. #2
    Modérateur
    Avatar de gangsoleil
    Homme Profil pro
    Manager / Cyber Sécurité
    Inscrit en
    Mai 2004
    Messages
    10 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Manager / Cyber Sécurité

    Informations forums :
    Inscription : Mai 2004
    Messages : 10 150
    Points : 28 129
    Points
    28 129
    Par défaut
    Bonjour,

    Pour remplacer une chaîne de caractère par une autre, il faut utiliser perl ou sed.

    Par exemple le code suivant remplace dans le fichier "mon_fichier.ext" la première chaine "chaine_a_remplacer" de chaque ligne par "nouvelle_chaine".
    Pour remplacer toutes les chaines de chaque ligne, il faut ajouter un g (pour 'global' après le dernier '/')

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    perl -pi -e "s/chaine_a_remplacer/nouvelle_chaine/" mon_fichier.ext
    Ensuite, pour faire ce que vous voulez, il vous faudra mettre cette ligne de code dans un script qui récupère la date dans le fichier, et la remplacer par la nouvelle, en faisant attention au fait que, par exemple, le lendemain du 30062007 n'est pas 31062007 mais 01072007...

  3. #3
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2007
    Messages : 3
    Points : 1
    Points
    1
    Par défaut Ok, mais comment passer une variable là-dednas
    Donc,

    ça marche super bien.
    il est possible dans le shell d'avoir la date du jour au format qui m'intéresse :

    date '+%m%d%Y'

    et aussi un jour de plus :

    date -d 'tomorrow' '+%m%d%Y'

    donc comment passer ces infos dans la ligne de commande ?

    Merci,

    Serge

  4. #4
    Membre éprouvé
    Avatar de SnakemaN
    Profil pro
    Bidouille-tout Android
    Inscrit en
    Juillet 2006
    Messages
    871
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Bidouille-tout Android

    Informations forums :
    Inscription : Juillet 2006
    Messages : 871
    Points : 1 118
    Points
    1 118
    Par défaut
    en essayant d'interpréter les commandes ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    perl -pi -e "s//date '+%m%d%Y'//date -d 'tomorrow' '+%m%d%Y'/" mon_fichier.ext
    voir

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    sed "s/{date '+%m%d%Y'}/{date -d 'tomorrow' '+%m%d%Y'}/g" mon_fichier.ext
    je teste pour voir...

    (edit) m'a suis trompé, mais ca marche pas des masses, la piste est a creuser..

  5. #5
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2007
    Messages : 3
    Points : 1
    Points
    1
    Par défaut humm petite erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     perl -pi -e "s//date '+%m%d%Y'//date -d 'tomorrow' '+%m%d%Y'/" monfichier_texte
    me donne l'erreur suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    String found where operator expected at -e line 1, near "'tomorrow' '+%m%d%Y'"
            (Missing operator before  '+%m%d%Y'?)
    syntax error at -e line 1, near "date -d "
    Execution of -e aborted due to compilation errors.

Discussions similaires

  1. Remplacer un caractère dans un fichier texte
    Par steelk dans le forum Excel
    Réponses: 36
    Dernier message: 29/01/2008, 16h10
  2. [MySQL] Remplacer en masse dans un fichier texte
    Par cadoudal56 dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 21/12/2006, 11h40
  3. Réponses: 10
    Dernier message: 29/04/2006, 11h40
  4. Réponses: 3
    Dernier message: 27/06/2005, 17h24
  5. Réponses: 5
    Dernier message: 25/01/2005, 15h26

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