1

我有一个inventory.txt包含数百行的文件。它列出了与客户 ID/名称、库存 ID/名称和产品 ID/名称相关的数据。customerId=123可能出现在任何给定行上的文件的一般设置 a 。在这条线之后,inventoryId=abc将出现一条线。这个文件看起来像这样:

<> START OF FILE
Customer ID=9000, Customer Name=Acme, Inc
Inventory ID=INV_ID1, Inventory Name=Acme_INV1
Product ID=100, Product Name=Banana
Product ID=200, Product Name=Apple
Inventory ID=INV_ID2, Inventory Name=Acme_INV2
Product ID=100, Product Name=Banana
Product ID=300, Product Name=Kiwi
Customer ID=7500, Customer Name=Anvil, Corp
Inventory ID=INV_ID3, Inventory Name=Anvil_INV1
Product ID=200, Product Name=Apple
<> END OF FILE

我想使用 SED 或任何运行良好的替代方法创建一个 CSV 格式的文件,该文件为每个客户/库存组合包含一行数据,其中仅包括客户 ID/名称和库存 ID/名称字段。所以输出看起来像:

"9000", "Acme, Inc.", "INV_ID1", "Acme_INV1"
"9000", "Acme, Inc.", "INV_ID2", "Acme_INV2"
"7500", "Anvil, Inc.", "INV_ID3", "Anvil_INV1"

我了解如何使用 SED 将输入数据格式化为带有逗号和引号的 CSV 文件输出,但我无法弄清楚如何强制Customer IDand在每一行Customer Name的开头重复。Inventory IDInventory Name

4

5 回答 5

2

这是一种使用方法awk

awk -F= '{ sub(/,.*/,"",$2) } /^Customer ID/ { r = $2 OFS $3 } /^Inventory ID/ { print "\"" r, $2, $3 "\"" }' OFS="\", \"" inventory.txt

sed解决方案:

sed -n '/^Customer ID/ h; /^Inventory ID/ { G; s/.*=\([^,]*\).*=\([^\n]*\).*=\([^,]*\).*=\(.*\)/"\3", "\4", "\1", "\2"/; p }' inventory.txt

结果:

"9000", "Acme, Inc", "INV_ID1", "Acme_INV1"
"9000", "Acme, Inc", "INV_ID2", "Acme_INV2"
"7500", "Anvil, Corp", "INV_ID3", "Anvil_INV1"

awk解释:

 OFS="\", \""          # set the output field separator to: ", "

-F=                    # split the line into three fields using the '=' character

{ sub(/,.*/,"",$2) }   # one each line of input, remove everything trailing a
                       # comma from field two.

/^Customer ID/ { ... } # if the line starts with 'Customer ID'; do

r = $2 OFS $3          # build a record using field two and three separated by 'OFS'

/^Inventory ID/ {...}  # if the line starts with 'Inventory ID'; do

print "\"" r, $2, $3 "\""   # print out a double-quote, the record, OFS, $2, OFS, 
                            # $3 and lastly a double quote

sed解释:

-n使用标志禁用默认打印。当一行以“客户 ID”开头时,复制该行以保留空间。当找到以“Inventory ID”开头的行时,将保留空间附加到当前行。使用一些神奇的正则表达式重新排列不同的字段并修复格式。

于 2013-01-11T14:28:47.993 回答
1

这可能对您有用(GNU sed):

sed -r '/^Customer/{h;d};/^Inventory/!d;G;s/.*=([^,]*).*=([^\n]*).*=([^,]*).*=(.*)/"\3", "\4", "\1", "\2"/' file
于 2013-01-11T15:13:21.460 回答
1

Perl 解决方案:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw(say);

my ($customer, $name);
while (<>) {
    if (/Customer ID=(.*), Customer Name=(.*)/) {
        ($customer, $name) = ($1, $2);
    } elsif (/Inventory ID=(.*), Inventory Name=(.*)/) {
        say join ', ' => map qq("$_"), $customer, $name, $1, $2;
    }
}
于 2013-01-11T14:17:56.340 回答
1

另一个不使用 FS 的 awk one-liner

awk -vq="\"" '/^(Cus|Inv)/{f=$0~/^Cus/;gsub(/[^,]*=/,q);sub(/,/,q",");c=f?$0q:c;if(!f)print c","$0q}' file

测试:

kent$  echo "Customer ID=9000, Customer Name=Acme, Inc
Inventory ID=INV_ID1, Inventory Name=Acme_INV1
Product ID=100, Product Name=Banana
Product ID=200, Product Name=Apple
Inventory ID=INV_ID2, Inventory Name=Acme_INV2
Product ID=100, Product Name=Banana
Product ID=300, Product Name=Kiwi
Customer ID=7500, Customer Name=Anvil, Corp
Inventory ID=INV_ID3, Inventory Name=Anvil_INV1
Product ID=200, Product Name=Apple"|awk -vq="\"" '/^(Cus|Inv)/{f=$0~/^Cus/;gsub(/[^,]*=/,q);sub(/,/,q",");c=f?$0q:c;if(!f)print c","$0q}'                                   
"9000","Acme, Inc","INV_ID1","Acme_INV1"
"9000","Acme, Inc","INV_ID2","Acme_INV2"
"7500","Anvil, Corp","INV_ID3","Anvil_INV1"
于 2013-01-11T14:57:42.973 回答
0

match()对函数使用 gawk 扩展

gawk '
    match($0, /^Customer ID=([^,]+), Customer Name=(.*)/, cust) {
        c_id=cust[1]; c_name=cust[2]
        next
    }
    match($0, /^Inventory ID=([^,]+), Inventory Name=(.*)/, inv) {
        printf "\"%s\",\"%s\",\"%s\",\"%s\"\n", c_id, c_name, inv[1], inv[2]
    }
' filename

输出

"9000","Acme, Inc","INV_ID1","Acme_INV1"
"9000","Acme, Inc","INV_ID2","Acme_INV2"
"7500","Anvil, Corp","INV_ID3","Anvil_INV1"
于 2013-01-11T16:30:01.887 回答