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

Shell et commandes GNU Discussion :

Récupérer entre un motif récurrent


Sujet :

Shell et commandes GNU

  1. #1
    Futur Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Décembre 2018
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 26
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2018
    Messages : 14
    Points : 7
    Points
    7
    Par défaut Récupérer entre un motif récurrent
    Bonjour,

    J'ai une ligne d'un fichier txt du type :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla
    Je souhaite récupérer :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    truc_important1/truc_important2/truc_important3
    Sachant que je ne connais pas à l'avance le nombre de truc important !

    J'ai essayer ceci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    sed -n 's/.*#\(.*\)@.*/\1/p'
    mais ça me donne juste le dernier, à savoir truc_important3

    des idées ?

  2. #2
    Expert éminent sénior Avatar de disedorgue
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Décembre 2012
    Messages
    4 280
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2012
    Messages : 4 280
    Points : 12 729
    Points
    12 729
    Par défaut
    Bonjour,

    Quelque chose comme ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $ echo 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla' | sed -e 's/\(^\|@\)[^#]*\(#\|$\)/\//g;s@^/\|/$@@g'
    truc_important1/truc_important2/truc_important3
    Cordialement.

  3. #3
    Futur Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Décembre 2018
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 26
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2018
    Messages : 14
    Points : 7
    Points
    7
    Par défaut
    Puis-je avoir une explication, je ne comprends pas tout ce qu'il y a dans le sed
    D'autant que cela de marche pas sur un fichier qui contient plusisuers lignes :c

  4. #4
    Expert éminent sénior Avatar de Flodelarab
    Homme Profil pro
    Inscrit en
    Septembre 2005
    Messages
    5 243
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente (Poitou Charente)

    Informations forums :
    Inscription : Septembre 2005
    Messages : 5 243
    Points : 13 458
    Points
    13 458
    Par défaut
    Bonjour

    À la barbare !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $ echo 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla' |awk -F'[#@]' '{for (i=2;i<=NF;i+=2) printf ((i>2)?"/":"")$i;print "";}'
    truc_important1/truc_important2/truc_important3
    Cette réponse vous apporte quelque chose ? Cliquez sur en bas à droite du message.

  5. #5
    Expert éminent sénior Avatar de disedorgue
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Décembre 2012
    Messages
    4 280
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2012
    Messages : 4 280
    Points : 12 729
    Points
    12 729
    Par défaut
    Citation Envoyé par Chadoxis Voir le message
    Puis-je avoir une explication, je ne comprends pas tout ce qu'il y a dans le sed
    D'autant que cela de marche pas sur un fichier qui contient plusisuers lignes :c
    ça ne marche pas, je ne comprends pas car

    Donne un exemple sur plusieurs ligne et ce que tu veux en sortie
    Cordialement.

  6. #6
    Futur Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Décembre 2018
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 26
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2018
    Messages : 14
    Points : 7
    Points
    7
    Par défaut
    j'ai :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla#truc_inportant4@blabla
    blabla#truc_important1@blabla
     
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla
    blabla#truc_important1@blabla#truc_important2@blabla
    et je voudrais

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    truc_important1/truc_important2/truc_important3
    truc_important1/truc_important2/truc_important3/truc_important4
    truc_important1
     
    truc_important1/truc_important2/truc_important3
    truc_important1/truc_important2

  7. #7
    Expert éminent sénior Avatar de Flodelarab
    Homme Profil pro
    Inscrit en
    Septembre 2005
    Messages
    5 243
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente (Poitou Charente)

    Informations forums :
    Inscription : Septembre 2005
    Messages : 5 243
    Points : 13 458
    Points
    13 458
    Par défaut
    Les deux commandes citées marchent parfaitement pour obtenir ce résultat.

    Peux-tu copier ta console ?

    Que donne la commande file sur ton fichier ?
    Cette réponse vous apporte quelque chose ? Cliquez sur en bas à droite du message.

  8. #8
    Expert éminent sénior Avatar de disedorgue
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Décembre 2012
    Messages
    4 280
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2012
    Messages : 4 280
    Points : 12 729
    Points
    12 729
    Par défaut
    Et en quoi il ne fonctionne pas le sed que je t'ai donné ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    $ cat /tmp/bob.txt
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla#truc_inportant4@blabla
    blabla#truc_important1@blabla
     
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla
    blabla#truc_important1@blabla#truc_important2@blabla
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    $ sed -e 's/\(^\|@\)[^#]*\(#\|$\)/\//g;s@^/\|/$@@g' /tmp/bob.txt
    truc_important1/truc_important2/truc_important3
    truc_important1/truc_important2/truc_important3/truc_inportant4
    truc_important1
     
    truc_important1/truc_important2/truc_important3
    truc_important1/truc_important2
    Un cas possible où ça peut poser souci avec les mêmes entrées, c'est que ton fichier soit au format non UNIX
    Que donne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $ file nom_du_fichier_a_traiter
    Cordialement.

  9. #9
    Expert éminent Avatar de BufferBob
    Profil pro
    responsable R&D vidage de truites
    Inscrit en
    Novembre 2010
    Messages
    3 035
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : responsable R&D vidage de truites

    Informations forums :
    Inscription : Novembre 2010
    Messages : 3 035
    Points : 8 400
    Points
    8 400
    Par défaut
    salut,

    avec grep :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    $ echo 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla' | grep -Po "#\K.+?(?=@)"
    truc_important1
    truc_important2
    truc_important3

  10. #10
    Expert confirmé
    Homme Profil pro
    Développeur informatique en retraite
    Inscrit en
    Avril 2008
    Messages
    2 101
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côtes d'Armor (Bretagne)

    Informations professionnelles :
    Activité : Développeur informatique en retraite

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 101
    Points : 5 849
    Points
    5 849
    Par défaut
    Je sais bien qu'on est censé être dans un forum linux, mais, avec mon bête sed POSIX (sous macos 10.14), je ferais:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    sed -e 's/^blabla#//' -e 's/@blabla$//' -e 's|@blabla#|/|g' plop
    truc_important1/truc_important2/truc_important3
    truc_important1/truc_important2/truc_important3/truc_inportant4
    truc_important1
     
    truc_important1/truc_important2/truc_important3
    truc_important1/truc_important2
    ou bien, si le motif n'est pas exactement "blabla":
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    sed -e 's/^[^#]*#//' -e 's/@[^@]*$//' -e 's|@[^#]*#|/|g' plop
    # ou:
    sed -e 's/^[^#]*#//;s/@[^@]*$//;s|@[^#]*#|/|g' plop
    car, si la commande awk proposée donne bien le bon résultat, ce n'est pas le cas de la commande sed proposée (toujours avec mon bête sed POSIX), qui ne marche pas (même avec l'option sed -E) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ sed -e 's/\(^\|@\)[^#]*\(#\|$\)/\//g;s@^/\|/$@@g' plop
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla#truc_inportant4@blabla
    blabla#truc_important1@blabla
     
    blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla
    blabla#truc_important1@blabla#truc_important2@blabla
    $ uname -a
    Darwin mon-mac 18.2.0 Darwin Kernel Version 18.2.0: Fri Oct  5 19:41:49 PDT 2018; root:xnu-4903.221.2~2/RELEASE_X86_64 x86_64
    Cela dit, si je remplace sed par gsed (le GNU sed que j'ai installé sur mon mac), alors ça marche!

  11. #11
    Expert éminent Avatar de BufferBob
    Profil pro
    responsable R&D vidage de truites
    Inscrit en
    Novembre 2010
    Messages
    3 035
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : responsable R&D vidage de truites

    Informations forums :
    Inscription : Novembre 2010
    Messages : 3 035
    Points : 8 400
    Points
    8 400
    Par défaut
    Citation Envoyé par jack-ft Voir le message
    (...) ne marche pas (même avec l'option sed -E)
    en fait -E il semblerait que ce soit non-documenté et l'équivalent BSD de l'option -r (extended regexp, mais pas encore suffisament extended pour avoir un opérateur non-gourmand...)

    ce truc là semble fonctionner, à vérifier néanmoins :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    $ sed -E 's/^[^#]*|#([^@]*)@[^#]*/\1\n/g' <<< 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla'
     
    truc_important1
    truc_important2
    truc_important3

  12. #12
    Expert éminent sénior Avatar de disedorgue
    Homme Profil pro
    Ingénieur intégration
    Inscrit en
    Décembre 2012
    Messages
    4 280
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur intégration
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2012
    Messages : 4 280
    Points : 12 729
    Points
    12 729
    Par défaut
    Ou une version posix en 2 cmd sed:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $ echo 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla' | sed --posix 's/@[^#]*#/\//g;s/.*#\(.*\)@.*/\1/g'
    truc_important1/truc_important2/truc_important3
    Le posix n'a pas l'air de supporter le ou "\|" .
    Cordialement.

  13. #13
    Futur Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Décembre 2018
    Messages
    14
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 26
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2018
    Messages : 14
    Points : 7
    Points
    7
    Par défaut
    Merci à tous pour votre aide ! Et bonne soirée !

  14. #14
    Expert confirmé
    Homme Profil pro
    Développeur informatique en retraite
    Inscrit en
    Avril 2008
    Messages
    2 101
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côtes d'Armor (Bretagne)

    Informations professionnelles :
    Activité : Développeur informatique en retraite

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 101
    Points : 5 849
    Points
    5 849
    Par défaut
    Citation Envoyé par disedorgue Voir le message
    Ou une version posix en 2 cmd sed:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $ echo 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla' | sed --posix 's/@[^#]*#/\//g;s/.*#\(.*\)@.*/\1/g'
    truc_important1/truc_important2/truc_important3
    Effectivement, ça marche sur un mac!

    Le posix n'a pas l'air de supporter le ou "\|" .
    Ben si... au moins parfois... mais sans le backslash:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $ echo coucou | sed -E -e 's/o|u/a/g'
    caacaa
    Citation Envoyé par BufferBob Voir le message
    ce truc là semble fonctionner, à vérifier néanmoins :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    $ sed -E 's/^[^#]*|#([^@]*)@[^#]*/\1\n/g' <<< 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla'
     
    truc_important1
    truc_important2
    truc_important3
    Bof... les \n ne marchent pas très bien:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $ sed -E 's/^[^#]*|#([^@]*)@[^#]*/\1\n/g' <<< 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla'
    ntruc_important1ntruc_important2ntruc_important3n
    La manière que j'ai trouvée est de les mettre explicitement (avec un backslash):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    $ sed -E 's/^[^#]*|#([^@]*)@[^#]*/\1\
    > /g' <<< 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla'
     
    truc_important1
    truc_important2
    truc_important3
    ou bien de les faire mettre par echo -e:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    $ sed -E "$(echo -e 's/^[^#]*|#([^@]*)@[^#]*/\1\\\n/g')" <<< 'blabla#truc_important1@blabla#truc_important2@blabla#truc_important3@blabla'
     
    truc_important1
    truc_important2
    truc_important3
    mais il ne faut pas oublier les guillemets et un bon gros backslash!

    C'est beau la portabilité...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [RegEx] Comment récupérer le texte entre deux motifs ?
    Par whitespirit dans le forum Langage
    Réponses: 6
    Dernier message: 25/03/2010, 21h59
  2. [bash]récupérer un code de retour
    Par GLDavid dans le forum Shell et commandes GNU
    Réponses: 1
    Dernier message: 25/04/2008, 14h16
  3. [BASH] récupérer stdin
    Par une maison en bois dans le forum Linux
    Réponses: 7
    Dernier message: 04/11/2007, 13h46
  4. [VBA Excel] identifier un motif récurrent
    Par Mourne dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 19/10/2007, 20h53
  5. Récupération de lignes entre 2 motifs
    Par ajsd0208 dans le forum Shell et commandes GNU
    Réponses: 2
    Dernier message: 31/05/2007, 16h15

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