6V8 - Production of my Mind

Home page > For your Computer > Découper des fichiers avec des expressions régulières

Découper des fichiers avec des expressions régulières

 

Je cherchais un port de csplit pour OS X. csplit est une commande Unix pour découper les fichiers sur différents critères. Il peut ainsi découper les fichiers sur toutes les lignes correspondants à une expression régulière.

Malheureusement, il n’y a rien dans Fink. j’ai donc écrit un petit script perl pour faire ce que je voulais.

13 July 2004, by Mortimer

Ce petit script est bien plus simple que csplit. Il permet seulement de découper l’entrée standard sur les lignes qui correspondent à l’expression régulière passée en paramètre.

  • regexp: une expression régulière perl,
  • [prefix]: par défaut, les fichiers de sorties sont appelés slice.0, ... , slice.n. Vous pouvez utiliser cette option pour changer le préfixe des fichiers.
  • -kl: par défaut, les lignes de séparation sont enlevées des fichiers de sortie. Utilisez cette option si vous voulez garder la ligne à la fin des fichiers.
  • -kf: par défaut, les lignes de séparation sont enlevées des fichiers de sortie. Utilisez cette option si vous voulez garder la ligne au début des fichiers.

Voici le code:


#!/usr/bin/perl -s

####################################################################
#       
#    This is a little perl script to split files in parts. Splitting is done at
#    each line containing a certain regular expression.
#
#    This is a simple implementation of one functionality of the Unix
#    csplit command.
#
#    Copyright (C) 2004  Pierre ANDREWS
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
####################################################################


if($#ARGV+1 == 0) {
        die "usage: myCsplit [-k] regexp [prefix]\n
split the file on matching lines. \n
\t regexp: a perl regular expression \n
\t [prefix]: by default, slice of the file are named slice.0, ..., slice.n
\t you can specify another prefix to be used.\n
\t -kl: by default the matching line is removed from output files.
\t Use this option if you want to keep it as the last line of each input file.\n
\t -kf: by default the matching line is removed from output files.
\t Use this option if you want to keep it as the first line of each input file.

"
;

}
$regexp = $ARGV[0];

if($#ARGV+1 > 1) {
        $prefix = $ARGV[1];
} else {
        $prefix = 'slice';
}

$count = 0;

open(OUTPUT,"> $prefix.0");

while(<STDIN>) {
        if($_ =~ /$regexp/) {
                if($kl) {
                        print OUTPUT $_
                }
                close OUTPUT;
                $count++;
                open(OUTPUT,"> $prefix.$count");
                if($kf) {
                        print OUTPUT $_
                }
        } else {
                print OUTPUT $_
        }
}
 
Date of online publication: 13 July 2004
last-update: 28 November 2007
All the versions of this article:
Forum messages 1
visits:
3615

Creative Commons Attribution NonCommercial ShareAlike 2.5  License
 

P.S.

Ce script est distribué sous license GPL.

1 Message

 

The most read articles

 
©
Pierre Andrews
York, uk
| Site Map | Site created with SPIP 1.9.2c [10268] | RSS | template by IZO, Mortimer. | clicky stats
Greek some similar this regarding
Greek some similar this regarding
Greek some similar this regarding
Greek some similar this regarding
Greek some similar this regarding