【问题标题】:Validate header of file in linux在linux中验证文件头
【发布时间】:2018-08-06 23:41:39
【问题描述】:

我正在使用下面的脚本来验证文件头。为此,我创建了一个只有标题的文件,并将其与另一个具有列数据和标题的文件进行比较。

awk -F"|" 'FNR==NR{hn=split($0,header); next}
     FNR==1 {n=split($0,fh)
            for(i=0;i<=hn; i++)
                if (fh[i]!=header[i]) {
                   printf "%s:order of %s is not correct\n",FILENAME, header[i]
                 next}
            if (hn==n)
                print FILENAME, "has expected order of fields"
        else
                print FILENAME, "has extra fields"
next
                }' key /Scripts/gst/Kenan_Test_Scenarios1.txt

示例文件头(键) SourceIdentifier|SourceFileName|GLAccountCode|Division|SubDivision|ProfitCentre1|ProfitCentre2|PlantCode|ReturnPeriod|SupplierGSTIN|DocumentType|SupplyType|DocumentNumber|DocumentDate|OriginalDocumentNumber|OriginalDocumentDate|CRDRPreGST|LineNumber|CustomerGSTIN|UINorComposition|OriginalCustomerGSTIN|CustomerName|CustomerCode|BillToState|ShipToState|POS|PortCode|ShippingBillNumber|ShippingBillDate|FOB|ExportDuty|HSNorSAC|ProductCode|ProductDescription|CategoryOfProduct|UnitOfMeasurement|Quantity|TaxableValue|IntegratedTaxRate|IntegratedTaxAmount|CentralTaxRate|CentralTaxAmount|StateUTTaxRate|StateUTTaxAmount|CessRateAdvalorem|CessAmountAdvalorem|CessRateSpecific|CessAmountSpecific|InvoiceValue|ReverseChargeFlag|TCSFlag|eComGSTIN|ITCFlag|ReasonForCreditDebitNote|AccountingVoucherNumber|AccountingVoucherDate|Userdefinedfield1|Userdefinedfield2|Userdefinedfield3

文件 2 标头连同数据(Kenan_Test_Scenarios1.txt)

SourceIdentifier|SourceFileName|GLAccountCode|Division|SubDivision|ProfitCentre1|ProfitCentre2|PlantCode|ReturnPeriod|SupplierGSTIN|DocumentType|SupplyType|DocumentNumber|DocumentDate|OriginalDocumentNumber|OriginalDocumentDate|CRDRPreGST|LineNumber|CustomerGSTIN|UINorComposition|OriginalCustomerGSTIN|CustomerName|CustomerCode|BillToState|ShipToState|POS|PortCode|ShippingBillNumber|ShippingBillDate|FOB|ExportDuty|HSNorSAC|ProductCode|ProductDescription|CategoryOfProduct|UnitOfMeasurement|Quantity|TaxableValue|IntegratedTaxRate|IntegratedTaxAmount|CentralTaxRate|CentralTaxAmount|StateUTTaxRate|StateUTTaxAmount|CessRateAdvalorem|CessAmountAdvalorem|CessRateSpecific|CessAmountSpecific|InvoiceValue|ReverseChargeFlag|TCSFlag|eComGSTIN|ITCFlag|ReasonForCreditDebitNote|AccountingVoucherNumber|AccountingVoucherDate|Userdefinedfield1|Userdefinedfield2|Userdefinedfield3
KEN|TEST1|||Tela|Outw|ANP|POST|1017|36AAA|NV|TX|4841446542|2017-12-12||2035-06-11|Y|1|36AAACB89|||||||36||||||94||Telecomm Servi||||1557.20|0.00|10.00|9.00|140.15|9.00|140.15|||||18.50||||||||B2B INV||

虽然两个文件中的标头相同,但低于输出并且不正确。

 is not correctnan_Test_Scenarios1.txt:order of Userdefinedfield3

能否帮我纠正一下代码,如果多个标题名称有msimatch,还需要捕获

【问题讨论】:

  • 你能在每个文件上显示cat -v 的结果吗?这将显示任何隐藏的控制字符、换行符等。我有一种感觉,你的一个文件是 DOS 格式,而另一个不是,或者类似的东西。这可以解释为什么即使可见字符相同,您的最后一个字段也不匹配。
  • 嗨,谢谢 Jas.. 文件中包含“^M”字符,现在我已将其删除并使脚本正常工作。但是还有一个问题。需要列出与另一个文件不匹配的所有标题。当前脚本仅显示它找到的第一个不匹配项,而不显示第二个不匹配项。 /Scripts/gst/Kenan_Test_Scenarios1.txt:SourceIdentifier 的顺序不正确
  • @as7951,如果您可以在 CODE TAGS {} 按钮中以更好的状态发布您的样品,我们会更容易为您提供帮助,请这样做。
  • 您是否真的需要识别不正确/顺序错误的标题,或者您是否担心确保数据的排序方式与“键”相同?因为这是两个略有不同的问题。
  • 要列出所有问题,只需去掉 next 语句。

标签: bash shell perl awk scripting


【解决方案1】:

好的,你已经标记了这个 perl,所以这里有一个 perl 答案。我认为您关注的是错误的问题 - 为什么不逐行读取,将它们解析为哈希,然后输出您想要的顺序:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

open ( my $first_file, '<', 'file_name_here' ) or die $!; 
chomp ( my @header = split /\|/, <$first_file> ); 
close ( $first_file ); 
#debugging
print Dumper \@header; 

open  ( my $second_file, '<', 'second_file_name_here' ) or die $!; 
chomp ( my @second_header = split /\|/, <$second_file> );

print join ( "|", @header ), "\n";
while ( <$second_file> ) {
    my %row;
    #use ordering of column headings to read into named fields; 
    @row{@second_header} = split /\|/;
    #debugging output to show you what's going on. 
    print Dumper \%row; 

    print join ("|", @row{@header} ), "\n";
}

这样你就不用在意订单是否错误,因为你会转发修复它。

如果您确实需要比较,那么您可以迭代每个 @header 数组并查找差异。但这更多的是一个关于你实际想要得到什么的问题——我建议你查看Array::Utils,因为这样你就可以轻松地使用array_diffintersectunique

【讨论】:

    【解决方案2】:

    这可能会派上用场

    $ diff -y --suppress-common-lines  <(tr '|' '\n' <file1) <(tr '|' '\n' <file2)
    

    将您的第一个文件按原样用于 file1 并使用此

    $ sed 's/2/8/;s/Export/Import/' file1 > file2
    

    创建第二个文件。运行脚本给出了

    ProfitCentre2                                                 | ProfitCentre8
    ExportDuty                                                    | ImportDuty
    

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 2021-03-11
      • 2021-04-29
      • 2015-05-02
      • 1970-01-01
      • 2011-06-23
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多