简体   繁体   English

在Linux环境中将CSV文件转换为XLS文件(SSR 8020 Ericsson)

[英]Convert CSV file to XLS file in Linux Environment (SSR 8020 Ericsson)

I have CPU_status.csv file and I am trying to convert it to a CPU_test.xls format using following script " ./Test.sh " in Linux VI (Ericsson SSR8020) but I am always getting one column in excel when I ftp out. 我有CPU_status.csv文件,我正在尝试使用Linux VI(Ericsson SSR8020)中的以下脚本“ ./Test.sh ”将其转换为CPU_test.xls格式,但是当我ftp out时,我总是在excel中获得一列。 Suggestion and feedback are appreciated. 建议和反馈表示赞赏。 Thank guys. 谢谢你们。

在此输入图像描述

 #!/bin/bash echo "Check CPU" tail -n +2 CPU_status.csv | head -1 | awk 'BEGIN{FS="|"}{print $1,$3,$16,$33,$50,$67,$81,$98}'| column -t;tail -n 50 CPU_status.csv | awk 'BEGIN{FS="|"}{print $1,$3,$16,$33,$50,$67,$81,$98}' | column -t echo "Hit Enter to Continue..." sleep 1 read continue echo "./Test.sh>CPU_test.xls" ./Test.sh>CPU_test.xls echo "Hit Enter to Continue..." sleep 1 read continue 

You can produce semicolon delimited file and then excel reads the data in the way you need and spreads them into succeeding columns. 您可以生成以分号分隔的文件,然后Excel以您需要的方式读取数据并将它们分散到后续列中。 Additionally remove column -t command. 另外删除column -t命令。

It shoul look like this: 看起来像这样:

tail -n +2 CPU_status.csv | head -1 | awk 'BEGIN{FS="|"; OFS=";"}{print $1,$3,$16,$33,$50,$67,$81,$98}'; tail -n 50 CPU_status.csv | awk 'BEGIN{FS="|"; OFS=";"}{print $1,$3,$16,$33,$50,$67,$81,$98}'

Perl has handy CPAN modules for reading and writing Excel .xls and xlsx. Perl有方便的CPAN模块,用于读写Excel .xls和xlsx。

#!/home/utils/perl-5.8.8/bin/perl
use Spreadsheet::WriteExcel;
use warnings;
use strict;
# open input file (fields delimited by ',')
my $inputfile = shift;
my $outputfile = shift;
open my $IN, '<', $inputfile, or die "Could not open $inputfile";
# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new($outputfile);
# Add a worksheet and a row
my $worksheet = $workbook->add_worksheet();
my $xlscol = my $xlsrow = 0;
for my $line (<$IN>) {
    print "\$line:  $line";
    my @fields = split /,/, $line;
    print "\@fields:  @fields\n";
    for my $csvcol (1, 3, 16, 33, 50, 67, 81, 98) {
        $xlscol++;
        print "$xlsrow\t$xlscol\t$csvcol\t$fields[$csvcol]\n";
        $worksheet->write($xlsrow, $xlscol, $fields[$csvcol]);
    }
    $xlsrow++;
}
# Usage:  writeXlsExample.pl CPU_status.csv my.xls
# File has been written to my.xls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM