J'ai des données (principalement des journaux, mais aussi des notes d'utilisateurs) sous forme de:
[2019_03_10][21:12:55] Very useful text of hight iportance to demonstrate my question. [2019_03_10][22:32:55] Another Text. [2019_03_10][23:02:22] blablabal Bla bla, just another lon g text with linebreak. And this one is just a little longer then those before.
Pour le moment, j'utilise cat test.txt | column -s '#' -t
et c'est ce que j'obtiens:
[2019_03_10][21:12:55] Very useful text of hight iportance to demonstrate my question. [2019_03_10][22:32:55] Another Text. [2019_03_10][23:02:22] blablabal Bla bla, just another lon g text with linebreak. And this one is just a little longer t hen those before.
C'est ce que j'aimerais obtenir:
[2019_03_10][21:12:55] # Very useful text of hight iportance to demonstrate my question. [2019_03_10][22:32:55] # Another Text. [2019_03_10][23:02:22] # blablabal Bla bla, just another long text with linebreak. And this one is just a little longer then those before.
5 Réponses :
Script Perl qui utilise le module standard Text :: Wrap :
$ perl -MText::Wrap -lpe '$_ = wrap("", "\t\t\t", $_); s/ # /\t/;' test.txt
Utilisation:
$ perl wrap.pl test.txt [2019_03_10][21:12:55] Very useful text of hight iportance to demonstrate my question.
Version une ligne:
#!/usr/bin/perl use strict; use warnings; use feature qw/say/; use Text::Wrap; while (<>) { chomp; my $line = wrap("", "\t\t\t", $_); $line =~ s/ # /\t/; say $line; }
Vous pouvez essayer le script suivant:
#!/bin/bash WIDTH=50 # width of column2 # TMPFILE=$(mktemp) # tempfile in /tmp TMPFILE=$(mktemp -p /dev/shm) # tempfile in shared memory while read line; do # read all lines from log column1=${line%%#*} # extract column1 blank=${column1//?/ } # blankline, size len(column1) column2=${line##*#} # column2, comments echo $column2 | fmt -$WIDTH > $TMPFILE # format column2 while read line2; do # read new formated lines echo "$column1" $line2 # write column1 and column2 column1=$blank # blank column1 done < $TMPFILE # read from tempfile done < "$1" # first arg from commandline rm $TMPFILE # delete tempfile
USAGE: scriptname logfile
Si vous n'avez pas de mémoire partagée, vous pouvez remplacer la ligne 4 par la ligne 3. p >
Utilisation de la ligne de commande Perl. Vous pouvez changer 20 en 30 ou 40 pour envelopper la longueur dont vous avez besoin.
$ perl -lne ' ($x,$y)=/(.+?)#(.+)/ ; printf("%s",$x); $s=""; while($y=~/(.{20,}?\s|.*$)/g) { $p=$1; print $s,$p if $p!~/^\s*$/s; $s="\t\t\t" } ' mac.txt [2019_03_10][21:12:55] Very useful text of hight iportance to demonstrate my question. [2019_03_10][22:32:55] Another Text. [2019_03_10][23:02:22] blablabal Bla bla, just another long text with linebreak. And this one is just a little longer then those before. $
Il y a une nouvelle ligne supplémentaire, si vous voulez supprimer cela
$ perl -ne ' ($x,$y)=/(.+?)#(.+)/ ; print "$x"; $s=""; while($y=~/(.{20,}?\s|.*$)/g) { printf("%s%s\n",$s,$1);$s="\t\t\t" } ' mac.txt [2019_03_10][21:12:55] Very useful text of hight iportance to demonstrate my question. [2019_03_10][22:32:55] Another Text. [2019_03_10][23:02:22] blablabal Bla bla, just another long text with linebreak. And this one is just a little longer then those before. $
Ce premier script répondra à votre question, en fractionnant les lignes au caractère 60 (sans tenir compte des espaces).
[2019_03_10][21:12:55] # Very useful text of hight importance to demonstrate my question. [2019_03_10][22:32:55] Another Text. [2019_03_10][23:02:22] blablabal Bla bla, just another long text with linebreak. And this one is just a lot longer then those before, by adding unsignificant and useless bla bla.
Sortira quelque chose comme:
sed ' :a; /.\{61\}/s/\([^\n]\{1,60\}\) \([^\n]\+\)/\1\n \2/; /\n/!bb; P; D; :b; ta '
sed ':a;/.\{61\}/s/\([^\n]\{1,60\}\) \([^\n]\+\)/\1\n \2/;/\n/!bb;P;D;:b;ta'
ou
[2019_03_10][21:12:55] # Very useful text of hight importanc e to demonstrate my question. [2019_03_10][22:32:55] Another Text. [2019_03_10][23:02:22] blablabal Bla bla, just another long text with linebreak. And this one is just a lot longer than those before, by adding unsignificant and useless b la bla.
affichera:
sed ':a;s/\([^\n]\{60\}\)\([^\n]\+\)/\1\n \2/;ta'
Je profiterais de la commande UNIX fold
pour que vous n'ayez pas à réinventer la roue:
$ cat tst.awk { beg = end = $0 sub(/ *#.*/,"",beg) sub(/[^#]+# */,"",end) cmd = "printf \047" end "\n\047 | fold -sw38" while ( (cmd | getline line) > 0 ) { print beg, line gsub(/./," ",beg) } } $ awk -f tst.awk file [2019_03_10][21:12:55] Very useful text of hight iportance to demonstrate my question. [2019_03_10][22:32:55] Another Text. [2019_03_10][23:02:22] blablabal Bla bla, just another long text with linebreak. And this one is just a little longer then those before.
Pourriez-vous s'il vous plaît fournir le contenu original du
test.txt
?