#!/usr/bin/perl use strict; use warnings; # this is the file we wish to have in a good format my $file = "$ARGV[0]"; my $Current_Dir = `pwd`; # print STDOUT "the current directory is $Current_Dir"; open(INFILE,"$ARGV[0]") or die "Can't open $ARGV[0]: $!"; # name of the OUTFILE # do not forget the "" if # never put a \n at the end of the OUTFILE name otherwise it does not create the output my ${outfile_name} = "bon_format_$file"; # to open the file # OUTFILE is the name of the HANDLE in this case open (OUTFILE, ">${outfile_name}.csv") or die "Can't open ${outfile_name}.csv: $!"; my @Parts; my $part; while () { # the lines are composed of elements separated by a point comma my $Line = $_; my @Elements = split(";", $Line); my $element = $Elements[1]; @Parts = split(" ",$element); print STDOUT "le septième élément est $Parts[6]\n"; my $longueur = @Parts; print STDOUT "le nombre d'éléments est $longueur\n"; # case where $Parts[5] is ADR2G and $Parts[6] is 2045 if (($Parts[5] eq /\w+\d+/) & ($Parts[6] eq /\d\d\d\d/)){ print OUTFILE "$Parts[5];$Parts[6]\n"; } # case where $Parts[6] is E and $Parts[7] is 6043 if (($Parts[6] eq /\w/) & ($Parts[7] eq /\d\d\d\d/)){ print OUTFILE "$Parts[6];$Parts[7]\n"; } # case where $Parts[6] is i, $Parts[7] is E and $Parts[8] is 6043 if (($Parts[6] eq /\w/) & ($Parts[7] eq /\w/) & ($Parts[8] eq /\d\d\d\d/)){ print OUTFILE "$Parts[6] $Parts[7];$Parts[8]\n"; } # case where $Parts[6] is E, $Parts[7] is i, $Parts[8] is U and $Parts[8] is 3065 if (($Parts[6] eq /\w/) & ($Parts[7] eq /\w/) & ($Parts[8] eq /\w/) & ($Parts[9] eq /\d\d\d\d/)){ print OUTFILE "$Parts[6] $Parts[7] $Parts[8];$Parts[9]\n"; } } close INFILE; close OUTFILE;