[UNIX] Découper un fichier par mot clé

Résolu
marco.marco Messages postés 30 Statut Membre -  
dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,

J'ai un problème plutot simple je pense pour les habitués du awk: je voudrais sous un systeme unix découper un fichier par mot clé.
Ce fichier se présente sous la forme suivante:

-----
AAA
-----

azerty
uiop
qsdfg
jklm

-----
AAA
-----

qwerty
poiuy
mlkjh

-----
BBB
-----

wqaze
wwxsdc
retyu

-----
BBB
-----

ertyu
dfuro

-----
AAA
-----

uiytr
vfrtyu
ffddsf

A chaque fois qu'il y a un "AAA" ou un "BBB" je voudrais donc qu'il me créé un fichier jusqu'au "AAA" ou "BBB" suivant.

J'ai trouvé un post sur le sujet que j'ai essayé d'appliquer à mon cas de figure mais ca ne marche pas:
awk '/^AAA/ || /^$/ {next} /^BBB/ {f=$2; next} {print > f}' $fichier
awk: (FILENAME=fichier.txt FNR=1) fatal: expression for '>' redirection has null string value

Pourriez vous me dire comment procéder ??

Mille mercis par avance!

3 réponses

  1. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 659
     
    hello
    tout les AAA vont dans le même fichier ou faut-il un nouveau fichier pour chaque bloc AAA?
    0
    1. marco.marco Messages postés 30 Statut Membre 1
       
      Oui, j'ai oublié de préciser: c'est bien un nouveau fichier pour chaque bloc AAA ou chaque bloc BBB.
      0
  2. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
     
    Salut,

    Et avec csplit ?

    csplit fichier "/AAA\|BBB/" {*}

    0
    1. marco.marco Messages postés 30 Statut Membre 1
       
      Tout simplement !!!
      Et moi qui croyait que le split se limitait à un découpage par numéro de ligne uniquement.

      Merci !!!!
      0
  3. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 659
     
    j'ajoute un nombre pour différencier les AAA
    awk '/^[A-Z]/ {f=$1; n++} /^[a-z]/ {print $0 > f "." n}'  fichier
    0
    1. marco.marco Messages postés 30 Statut Membre 1
       
      Dubcek, j'ai lancé ta commande mais elle ne créé des fichiers ne contenant qu'une seule ligne à savoir le séparateur AAA ou BBB et pas ce qui suit jusqu'au prochain AAA/BBB
      ls -rtl
      -rw-r--r-- 1 root root 47 nov 29 11:23 AAA.13
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.14
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.15
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.16
      -rw-r--r-- 1 root root 45 nov 29 11:23 AAA.17
      -rw-r--r-- 1 root root 45 nov 29 11:23 AAA.18
      -rw-r--r-- 1 root root 45 nov 29 11:23 AAA.19
      -rw-r--r-- 1 root root 45 nov 29 11:23 AAA.20
      -rw-r--r-- 1 root root 37 nov 29 11:23 AAA.21
      -rw-r--r-- 1 root root 42 nov 29 11:23 AAA.22
      -rw-r--r-- 1 root root 45 nov 29 11:23 AAA.23
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.24
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.25
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.26
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.27
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.28
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.29
      -rw-r--r-- 1 root root 46 nov 29 11:23 AAA.30
      -rw-r--r-- 1 root root 44 nov 29 11:23 AAA.31

      # cat AAA.13
      AAA : test [AIX]
      #

      De toute facon je vais utiliser le csplit de zipe31 qui marche très bien.
      Merci tout de meme :-)
      0
    2. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 659
       
      ah ?
      $ awk '/^[A-Z]/ {f=$1; n++} /^[a-z]/ {print $0 > f "." n}' fichier
      $ ls A* B*
      AAA.1 AAA.2 AAA.5 BBB.3 BBB.4
      $ more A* B*
      ::::::::::::::
      AAA.1
      ::::::::::::::
      azerty
      uiop
      qsdfg
      jklm
      ::::::::::::::
      AAA.2
      ::::::::::::::
      qwerty
      poiuy
      mlkjh
      ::::::::::::::
      AAA.5
      ::::::::::::::
      uiytr
      vfrtyu
      ffddsf
      ::::::::::::::
      BBB.3
      ::::::::::::::
      wqaze
      wwxsdc
      retyu
      ::::::::::::::
      BBB.4
      ::::::::::::::
      ertyu
      dfuro
      $
      0
    3. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 659
       
      essayer
      awk '/^---/ {getline; f=$1 "." ++n; getline} /^[a-z]/ {print $0 > f}'  fichier
      0