Lire un fichier texte colonne par colonne

Résolu
houba91 Messages postés 11 Date d'inscription vendredi 21 mars 2014 Statut Membre Dernière intervention 21 mai 2014 - Modifié par cs_Julien39 le 28/04/2014 à 09:39
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 2 avril 2014 à 09:20
Bonjour,
j'ai besoin d'aide. En fait j'ai un fichier log sous format txt voici un morceau

1394950584.861 438 10.0.149.23 TCP_MISS/200 934 GET http://dzayfdqe.trwvksnpc.au/lsetyumxs.html - DIRECT/69.114.1.230 text/html
1394950629.431 580 10.0.149.23 TCP_MISS/200 922 GET http://oacsyus.jxjsaz.tz/znvr.html - DIRECT/79.179.220.28 text/html
1394950629.862 460 10.0.149.23 TCP_MISS/200 1860 GET http://jfgmbt.pfkaa.cat/ogwblsah.html - DIRECT/219.135.237.57 text/html
1394950632.629 849 10.0.149.23 TCP_MISS/200 607 GET http://qpslzh.ngqde.construction/yhhbzllx.html - DIRECT/202.34.56.241 text/html
1394950633.244 916 10.0.149.23 TCP_MISS/200 1651 GET http://psficabij.hklrmkfm.mv/vqjfld.html - DIRECT/154.252.249.81 text/html
1394950633.687 906 10.0.149.23 TCP_MISS/200 182 GET http://weiovu.vlscqtzwh.sz/mltwecfgg.html - DIRECT/176.245.255.69 text/html
1394950636.311 678 10.0.149.23 TCP_MISS/200 1839 GET http://iwcrr.nfveakyas.mp/ayusm.html - DIRECT/152.85.2.88 text/html
je souhaite découper chaque ligne en des champs par exemple
pour la ligne 1 je voudrais avoir le champs ClientIP qui 10.0.149.23 et les champs serveur et la page demandé
J'ai pensé à écrire une expression régulière pour chaque ligne voila l'expression
([0-9]{10})(.[0-9]{3})( +)([0-9]+)( +)([0-9]\\d{0,2}\\.){3}[0-9]\\d{0,2}( +)([A-Z]+)(_[A-Z]+)(/[0-9]+)( +)([0-9]+)( +)(GET)( +)(http://)([a-z]+)(.[a-z]+)(.[a-z]+)(/[a-z]+)(.html)( +)(-)( +)([A-Z]+)(/([0-9]\\d{0,2}\\.){3}[0-9]\\d{0,2})( +)([a-z]+)(/html)
svp aidez moi comment faire

1 réponse

Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
2 avril 2014 à 09:20
Salut,

Si tu veux simplement segmenter tes champs tu peux faire un split(" ") (ou utiliser un StringTokenizer) sur ta ligne et du coup tous les champs seront dans une dimension de ton split.

public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new File("thefile.txt"));
String line;
String [] split;
while(sc.hasNextLine()) {
line = sc.nextLine();
split = line.split("\\s");
for(String sp : split)
System.out.println("==>" + sp);
System.out.println();
}
sc.close();
}

Cela dit il est possible d'appliquer ta regex (en java l'espace est représenté par \s soit \\s dans une string) mais pas d'un bloc. Il te faut décomposer la regex.

// il y a surement beaucoup plus beau/simple que ma solution
import java.io.File;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

public static void main(final String[] args) throws Exception {
final Pattern pp[] = {
Pattern.compile("([0-9]{10})(.[0-9]{3})"),
Pattern.compile("([0-9]+)"),
Pattern.compile("([0-9]\\d{0,2}\\.){3}[0-9]\\d{0,2}"),
Pattern.compile("([A-Z]+)(_[A-Z]+)(/[0-9]+)"),
Pattern.compile("([0-9]+)"),
Pattern.compile("(GET)"),
Pattern.compile("(http://)([a-z]+)(.[a-z]+)(.[a-z]+)(/[a-z]+)(.html)"),
Pattern.compile("(-)"),
Pattern.compile("([A-Z]+)(/([0-9]\\d{0,2}\\.){3}[0-9]\\d{0,2})"),
Pattern.compile("([a-z]+)(/html)")
};

final Scanner sc = new Scanner(new File("thefile.txt"));
String line;
int i;
while (sc.hasNextLine()) {
line = sc.nextLine();
i = 0;
Matcher match = pp[i].matcher(line);
while (match.find() && i < pp.length - 1) {
System.out.println("==> " + match.group());
match = match.usePattern(pp[++i]);
}
}
sc.close();
}
}
1
Rejoignez-nous