Преобразование файла XLS в файл CSV в Perl без использования электронной таблицы [закрыто]

Al menos con GNU sedy suponiendo que sus campos no pueden contener separadores de coma incrustados , podría hacer

sed 's/,/\n/6; P; D' roll.txt

que intenta repetidamente reemplazar la sexta coma con una nueva línea, imprimir y luego eliminar la parte del espacio del patrón hasta la nueva línea.

NOTA :no es necesario implementar una prueba/bifurcación etiquetada explícitamente, ya que el comando Dimplícitamente "reinicia el ciclo" en el resto de la línea:

D
If pattern space contains no newline, start a normal new cycle as if the d command was issued. Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the resultant pattern space, without reading a new line of input.

(crédito a @RakeshSharma por aclarar esto ).

Ej.

sed 's/,/\n/6; P; D' roll.txt 
'123456789','987651234','129873645','213456789','987612345','543216789'
'432156789','876543291','213465789','542637819','123456','23456'
'22234','3456','7890543','34567891,'2345','567'

Alternativamente, con el módulo Text::CSVde Perl:

perl -MText::CSV -ne '
  BEGIN{$p = Text::CSV->new()} 
  @fields = $p->fields() if $p->parse($_); 
  do {
    print join ",", splice @fields, 0, 6; print "\n";
  } while @fields
' roll.txt
'123456789','987651234','129873645','213456789','987612345','543216789'
'432156789','876543291','213465789','542637819','123456','23456'
'22234','3456','7890543','34567891,'2345','567'
1
28.06.2019, 20:51
1 ответ

Установите perl , если он еще не установлен, затем выполните скрипт xls2csv с помощью команды, аналогичной:

xls2csv -x "source.xls" -c "destination.csv" 
0
28.01.2020, 00:09

Теги

Похожие вопросы