[英]Write linux output to a specific column/row in an XLS or CSV file
[英]Convert CSV file to XLS file in Linux Environment (SSR 8020 Ericsson)
我有CPU_status.csv文件,我正在嘗試使用Linux VI(Ericsson SSR8020)中的以下腳本“ ./Test.sh ”將其轉換為CPU_test.xls格式,但是當我ftp out時,我總是在excel中獲得一列。 建議和反饋表示贊賞。 謝謝你們。
#!/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
您可以生成以分號分隔的文件,然后Excel以您需要的方式讀取數據並將它們分散到后續列中。 另外刪除column -t
命令。
看起來像這樣:
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有方便的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.