2009-05-12 8 views
0

je avais besoin de convertir un onglet Seperated fichier texte dans un format de tableau comme suitTab texte Seperated converti en format tableau avec bordure

contenu du fichier

ID<TAB>WorkId<TAB>Date 
0<TAB>W-1230699600000<TAB>2008-12-31 
1<TAB>W-1233378000000<TAB>2009-01-31 

doit devenir

+--------+-----------------+-------------+ 
| ID  | WorkId   | Date  | 
+--------+-----------------+-------------+ 
| 0  | W-1230699600000 | 2008-12-31 | 
| 1  | W-1233378000000 | 2009-01-31 | 
+--------+-----------------+-------------+ 
+1

Juste un petit reproche - si elle est tabulé, il n'est pas CSV. –

+0

Juste fixé que merci –

Répondre

1

J'ai fini par utiliser awk pour faire ça. Voici ce que j'ai fait.

cat /tmp/1.txt | awk -F'\t' ' 
BEGIN { 
    fieldSize=5; 

    for (i=1; i < fieldSize; i++) { 
    fLength[i]=0; 
    } 
} 
{ 
    for (i=1; i <= fieldSize; i++) { 
    if (fLength[i] < length($i)) { 
      fLength[i] = length($i) 
     } 

    } 
    line[NR]=$0 
} 
END { 
    for (i=1; i <= NR; i++) { 
     split(line[i], values, "\t") 
     format="| " 
     arguments="" 
     seperator="+" 
     for (j=1; j <= fieldSize; j++) { 
     format=format"%-"fLength[j]"s | " 
      for (c=0; c <= fLength[j]; c++) seperator=seperator"-" 
      seperator=seperator"-+" 
      arguments=arguments" \""values[j]"\"" 
     } 

    if (i == 1) print seperator; 

     printCommand="printf \""format"\n\" "arguments 
     system(printCommand) 

    if (i == 1) print seperator; 
    } 
    print seperator; 

} 
' 
0

de formatter Utiliser Perl:

#!/usr/bin/perl 
open(MYFILE, ">myfile.txt") or die "File does not exist!"; 

my $id, $workid, $date; 

print MYFILE 
"+-----+----------+---------------+\n" . 
"|ID |WorkId |Date   |\n" . 
"+-----+----------+---------------+\n"; 

format MYFILE = 
|@<<<<|@<<<<<<<<<|@<<<<<<<<<<<<<<| 
$id $workid  $date 
+-----+----------+---------------+ 
. 

while(<>) 
{ 
    next if (m/^ID/); 
    ($id,$workid,$date) = split(/\t/); 
    write MYFILE; 
} 
Questions connexes