简体   繁体   English

如何使用perl在csv文件中插入空列

[英]How to insert empty column in a csv file using perl

I have a list of CSV file i need to add number of empty column on each, the number of empty column i need to add change on each file in a linear way, first file i want to insert 0 column, second file i want to insert 1 column, third file i want to insert 2 column and so on, the column should be inserted always as second one. 我有一个CSV文件列表我需要在每个上添加空列数,空列数我需要以线性方式添加每个文件的变化,第一个文件我要插入0列,第二个文件我想要插入1列,第三个文件我要插入2列,依此类推,该列应始终插​​入第二列。 all the input file are a 2 column csv as the example below 所有输入文件都是2列csv,如下例所示

file 1 档案1

2016/03/07 23:42:40.618 GMT,54.5
2016/03/07 23:43:40.618 GMT,54.0771
2016/03/07 23:44:40.618 GMT,53.9472
2016/03/07 23:45:40.618 GMT,54.2914

file 2 档案2

2016/03/07 23:42:40.618 GMT,49.013
2016/03/07 23:43:40.618 GMT,48.688
2016/03/07 23:44:40.618 GMT,47.7052 
2016/03/07 23:45:40.618 GMT,47.9057

file3 文件3

2016/03/07 23:51:40.618 GMT,50.7858
2016/03/07 23:52:40.618 GMT,52.5267
2016/03/07 23:53:40.618 GMT,54.2865
2016/03/07 23:54:40.618 GMT,53.2014
2016/03/07 23:55:40.618 GMT,52.0538

etc. 等等

I want the output to be 我想要输出

file1 文件1

2016/03/07 23:42:40.618 GMT,54.5
2016/03/07 23:43:40.618 GMT,54.0771
2016/03/07 23:44:40.618 GMT,53.9472
2016/03/07 23:45:40.618 GMT,54.2914

File2 文件2

2016/03/07 23:42:40.618 GMT,,49.013
2016/03/07 23:43:40.618 GMT,,48.6883
2016/03/07 23:44:40.618 GMT,,47.7052
2016/03/07 23:45:40.618 GMT,,47.9057

file3 文件3

2016/03/07 23:51:40.618 GMT,,,50.7858
2016/03/07 23:52:40.618 GMT,,,52.5267
2016/03/07 23:53:40.618 GMT,,,54.2865
2016/03/07 23:54:40.618 GMT,,,53.2014
2016/03/07 23:55:40.618 GMT,,,52.0538

This is the code wrote but it generate the same output for all file with added just one empty column, probably i'm missing something silly but i can't get what 这是代码写的,但它为所有文件生成相同的输出,只添加了一个空列,可能我错过了一些愚蠢但我无法得到什么

#!/usr/bin/perl
#use strict; 
#use diagnostics;
use CGI qw(:standard);
use Cwd;
use File::Find;
use File::Basename;
use Text::CSV;
use Text::Trim qw(trim);
use List::MoreUtils qw(uniq);
use Text::CSV_XS;
sub read_dir{
    my $ph=$_[0];
    my $match=$_[1];
    my @fl = ( );
    #print "<b> will open $ph";
    opendir DH, "$ph" or die "Cannot open Dir $ph: $!";
    #if ($match eq "") {
    @fl = grep !/^\.\.?$\.*/, sort(readdir DH) ;
    #}
    #else {
    #   my @fl = grep {$_ =~ $match} sort(readdir DH) ;
    #}
    closedir DH;
    if ($match ne "") {
        foreach my $file (@fl){
            if ($file =~$match) {
                push(@ffl,$file);
            }
        }
        return @ffl;
    }
    else {
        return @fl;
    }
}
@column=();
$loc="/myfolder";
$offset=1;
@sites=("dir1","dir2","dir3");
$s="subdir";
foreach $t (@sites) {
    $m=0; #number of empty column to insert in the file
    @list=read_dir("$loc/Statistics/$t/$s"); #directory that contain the csv file to manipulate
    foreach $l (@list) {
        #inser the empty column 
        open $in, "<","$loc/Statistics/$t/$s/$l" or die $!;
        open $out, ">>", "$loc/Statistics/$t/$s/$l.tmp" or die $!;
        foreach $r (0..$m) {
            print "<br> r is $r";
            open $out, ">>", "$loc/Statistics/$t/$s/$l.tmp" or die $!;
            while ($row = $ccsv->getline($in)) {
                splice @$row, $offset, 0, shift @column;
                $ccsv->print($out, $row);
            }
            close $out;
        }
        close $in;
        $m=$m+1;
    }
}
perl -i~ -pe '
    s/,/"," x (1 + $commas)/e;
    $commas++ if eof;
' -- file1 file2 file3
  • -p reads the file line by line, after running the code, it prints it -p逐行读取文件,运行代码后打印出来
  • -i~ changes the input files "in place", saving a backup as file1~ etc. -i~将输入文件“就地”更改,将备份保存为file1~等。
  • s/,/"," x $commas/e substitutes a comma with evaluated code in the replacement part, ie the comma repeated one time for the first file, two times for the second, and so on s/,/"," x $commas/e用替换部分中的评估代码替换逗号,即逗号对第一个文件重复一次,对第二个文件重复两次,依此类推
  • eof returns true every time an input file ends, see eof 每次输入文件结束时, eof返回true,请参阅eof
  • $commas++ adds 1 to $commas $commas++在$逗号中加1

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

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