Linux 在 AWK 脚本输出中的字段周围添加双引号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14427188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:44:21  来源:igfitidea点击:

Add double quotes around fields in AWK script output?

linuxshellmagentoawk

提问by John Steensen

I have written an awk script that converts a distributor flatfile into a CSV importable into Magento. This file is semi-colon delimited.

我编写了一个 awk 脚本,将分发器平面文件转换为可导入 Magento 的 CSV。此文件以分号分隔。

It is not putting quotes around each field like the importer requires. It works fairly well, but is causing some issues on the data import without the enclosing double quotes. I spent a couple hours trying to figure out how to add this to the existing script, without much luck. Any help would be greatly appreciated - I am pretty new to AWK.

它没有像进口商要求的那样在每个字段周围加上引号。它工作得相当好,但在没有双引号的情况下导致数据导入出现一些问题。我花了几个小时试图弄清楚如何将它添加到现有脚本中,但运气不佳。任何帮助将不胜感激 - 我对 AWK 很陌生。

Current Output

电流输出

store;websites;attribute_set;type;category_ids;sku;has_options;name;meta_title;meta_description;image;small_image;thumbnail;url_key;url_path;config_attributes;custom_design;page_layout;options_container;country_of_manufacture;msrp_enabled;msrp_display_actual_price_type;gift_message_available;rsr_pn;manufacturer_pn;price;special_price;cost;weight;msrp;status;visibility;manufacturer;enable_googlecheckout;tax_class_id;is_recurring;description;short_description;meta_keyword;custom_layout_update;news_from_date;news_to_date;special_from_date;special_to_date;custom_design_from;custom_design_to;qty;min_qty;use_config_min_qty;is_qty_decimal;backorders;use_config_backorders;min_sale_qty;use_config_min_sale_qty;max_sale_qty;use_config_max_sale_qty;is_in_stock;low_stock_date;notify_stock_qty;use_config_notify_stock_qty;manage_stock;use_config_manage_stock;stock_status_changed_auto;use_config_qty_increments;qty_increments;use_config_enable_qty_inc;enable_qty_increments;is_decimal_divided;stock_status_changed_automatically;use_config_enable_qty_increments;product_name;store_id;product_type_id;product_status_changed;product_changed_websites;gallery;related;upsell;crosssell;tier_prices;associated;bundle_options;grouped;group_price_price;downloadable_options;super_attribute_pricing;product_tags
admin;base;Default;simple;2,35,36;844802016148;0;5.11 HOLSTER SHIRT L WHITE;;;/5/1/511-40011-010-L_1.jpg;/5/1/511-40011-010-L_1.jpg;/5/1/511-40011-010-L_1.jpg;511-40011-010-L;511-40011-010-L.html;;;No layout updates;Block after Info Column;;Use config;Use config;No;511-40011-010-L;40011;74.99;;48.00;5;74.99;Enabled;Catalog, Search;5.11 Tactical;Yes;Taxable Goods;No;5.11 Tactical Short Sleeve Shirt L White Holster Shirt Crew 40011;5.11 Tactical Short Sleeve Shirt L White Holster Shirt Crew 40011;;;;;;;;;0;0;1;0;0;1;1;1;0;1;1;;;1;0;1;0;1;0;1;0;0;0;1;5.11 HOLSTER SHIRT L WHITE;0;simple;;;;;;;;;;;;;;

Desired Output

期望输出

"store";"websites";"attribute_set";"type";"category_ids";"sku";"has_options";"name";"meta_title";"meta_description";"image";"small_image";"thumbnail";"url_key";"url_path";"config_attributes";"custom_design";"page_layout";"options_container";"country_of_manufacture";"msrp_enabled";"msrp_display_actual_price_type";"gift_message_available";"rsr_pn";"manufacturer_pn";"price";"special_price";"cost";"weight";"msrp";"status";"visibility";"manufacturer";"enable_googlecheckout";"tax_class_id";"is_recurring";"description";"short_description";"meta_keyword";"custom_layout_update";"news_from_date";"news_to_date";"special_from_date";"special_to_date";"custom_design_from";"custom_design_to";"qty";"min_qty";"use_config_min_qty";"is_qty_decimal";"backorders";"use_config_backorders";"min_sale_qty";"use_config_min_sale_qty";"max_sale_qty";"use_config_max_sale_qty";"is_in_stock";"low_stock_date";"notify_stock_qty";"use_config_notify_stock_qty";"manage_stock";"use_config_manage_stock";"stock_status_changed_auto";"use_config_qty_increments";"qty_increments";"use_config_enable_qty_inc";"enable_qty_increments";"is_decimal_divided";"stock_status_changed_automatically";"use_config_enable_qty_increments";"product_name";"store_id";"product_type_id";"product_status_changed";"product_changed_websites";"gallery";"related";"upsell";"crosssell";"tier_prices";"associated";"bundle_options";"grouped";"group_price_price";"downloadable_options";"super_attribute_pricing";"product_tags"
"admin";"base";"Default";"simple";"2,35,36";"844802016148";"0";"5.11 HOLSTER SHIRT L WHITE";"";"";"/5/1/511-40011-010-L_1.jpg";"/5/1/511-40011-010-L_1.jpg";"/5/1/511-40011-010-L_1.jpg";"511-40011-010-L";"511-40011-010-L.html";"";"";"No layout updates";"Block after Info Column";"";"Use config";"Use config";"No";"511-40011-010-L";"40011";"74.99";"";"48.00";"5";"74.99";"Enabled";"Catalog, Search";"5.11 Tactical";"Yes";"Taxable Goods";"No";"5.11 Tactical Short Sleeve Shirt L White Holster Shirt Crew 40011";"5.11 Tactical Short Sleeve Shirt L White Holster Shirt Crew 40011";"";"";"";"";"";"";"";"";"0";"0";"1";"0";"0";"1";"1";"1";"0";"1";"1";"";"";"1";"0";"1";"0";"1";"0";"1";"0";"0";"0";"1";"5.11 HOLSTER SHIRT L WHITE";"0";"simple";"";"";"";"";"";"";"";"";"";"";"";"";"";"

Script - rsrimport.awk

脚本 - rsrimport.awk

#!/bin/awk -f
# ----------------------------------------------------------------------------------------
# Copyright (c) 2012 - 2013 John Steensen <[email protected]>
# All rights reserved. No warranty, explicit or implicit, provided.
# ----------------------------------------------------------------------------------------
# AWK Processing
# Updated 03DEC2012@1552 MST
# ----------------------------------------------------------------------------------------
# Warnings/Dependancy Notes
# AWK
# ----------------------------------------------------------------------------------------
BEGIN {
FS=";";
OFS=";";
CATEGORY="47";
IMAGE="imagepathfail";
URLKEY="urlkeyfail";
URLPATH="urlpathfail";
print "store", "websites", "attribute_set", "type", "category_ids", "sku", "has_options", "name", "image", "small_image", "thumbnail", "url_key", "url_path", "page_layout", "options_container", "msrp_enabled", "msrp_display_actual_price_type", "gift_message_available", "rsr_pn", "manufacturer_pn", "price", "cost", "weight", "msrp", "manufacturer", "status", "is_recurring", "visibility", "enable_googlecheckout", "tax_class_id", "description", "short_description", "qty", "min_qty", "use_config_min_qty", "is_qty_decimal", "backorders", "use_config_backorders", "min_sale_qty", "use_config_min_sale_qty", "max_sale_qty", "use_config_max_sale_qty", "is_in_stock", "notify_stock_qty", "use_config_notify_stock_qty", "manage_stock", "use_config_manage_stock", "stock_status_changed_auto", "use_config_qty_increments", "qty_increments", "use_config_enable_qty_inc", "enable_qty_increments", "is_decimal_divided", "stock_status_changed_automatically", "use_config_enable_qty_increments", "product_name", "store_id", "product_type_id";
}
{
# DEFINE CATEGORY
if (=="1") CATEGORY="2,3,4";
else if (=="2") CATEGORY="2,3,7";
else if (=="3") CATEGORY="2,3,8";
else if (=="4") CATEGORY="2,3,22,23";
else if (=="5") CATEGORY="2,3,5";
else if (=="7") CATEGORY="2,3,6";
else if (=="8") CATEGORY="2,27,28";
else if (=="9") CATEGORY="2,27,29";
else if (=="10") CATEGORY="2,9,13";
else if (=="11") CATEGORY="2,9,14";
else if (=="12") CATEGORY="2,35,38";
else if (=="13") CATEGORY="2,9,16";
else if (=="14") CATEGORY="2,35,37";
else if (=="15") CATEGORY="2,19,21";
else if (=="16") CATEGORY="2,9,15";
else if (=="17") CATEGORY="2,9,16";
else if (=="18") CATEGORY="2,19,20";
else if (=="20") CATEGORY="2,27,33";
else if (=="21") CATEGORY="2,9,17";
else if (=="22") CATEGORY="2,3,22,24";
else if (=="23") CATEGORY="2,3,22,25";
else if (=="24") CATEGORY="2,9,13";
else if (=="25") CATEGORY="2,40,43";
else if (=="26") CATEGORY="2,40,44";
else if (=="27") CATEGORY="2,3,22,26";
else if (=="28") CATEGORY="2,27,31";
else if (=="29") CATEGORY="2,27,32";
else if (=="30") CATEGORY="2,27,30";
else if (=="31") CATEGORY="2,27,34";
else if (=="32") CATEGORY="2,9,11";
else if (=="33") CATEGORY="2,35,36";
else if (=="34") CATEGORY="2,9,10";
else if (=="35") CATEGORY="2,9,18";
else if (=="36") CATEGORY="2,40,42";
else if (=="38") CATEGORY="2,40,41";
else if (=="39") CATEGORY="2,40,45";
else if (=="40") CATEGORY="2,35,39";
else if (=="41") CATEGORY="2,9,12";
else if (=="43") CATEGORY="2,9,12";
else if (=="01") CATEGORY="2,3,4";
else if (=="02") CATEGORY="2,3,7";
else if (=="03") CATEGORY="2,3,8";
else if (=="04") CATEGORY="2,3,22,23";
else if (=="05") CATEGORY="2,3,5";
else if (=="07") CATEGORY="2,3,6";
else if (=="08") CATEGORY="2,27,28";
else if (=="09") CATEGORY="2,27,29";
else CATEGORY="47";
# DEFINE IMAGE WITH PATH.
IMAGE="/5/1/""_1.jpg";
# DEFINE URL KEY
URLKEY=;
# DEFINE URL PATH
URLPATH=".html";
print "admin", "base", "Default", "simple", CATEGORY, , "0", , IMAGE, IMAGE, IMAGE, URLKEY, URLPATH, "No layout updates", "Block after Info Column", "Use config", "Use config", "No", , , , , , , , "Enabled", "No", "Catalog, Search", "Yes", "Taxable Goods", , , , "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "0", "0", "1", , "0", "simple";
}
END {}

采纳答案by Nikolai Popov

If you want:

如果你想:

add this to the existing script.

将此添加到现有脚本中。

You can insert additional \"\"in each argument of printlike this:

您可以\"\"在每个参数中插入附加内容,print如下所示:

print "\"admin\"", "\"base\"", ...

print "\"admin\"", "\"base\"", ...

Edited:

编辑:

Yes, perhaps seting OFS is better solution:

是的,也许设置 OFS 是更好的解决方案:

BEGIN { OFS="\";\""; } ... print "\"admin", ...., "simple\"";

BEGIN { OFS="\";\""; } ... print "\"admin", ...., "simple\"";

回答by Dmitri Chubarov

To add quotes around the entries you could use a simple AWK loop:

要在条目周围添加引号,您可以使用简单的 AWK 循环:

Script - simple_loop.awk

脚本 - simple_loop.awk

BEGIN {FS=";"}
{
  for(i=1;i<NF;i++){
       printf("\"%s\";", $i); 
  }
  printf("\"%s\"\n",$NF);
}

For instance

例如

echo "admin;base;5.11 HOLSTER SHIRT L WHITE;;" | awk -f simple_loop.awk

Should output

应该输出

"admin";"base";"5.11 HOLSTER SHIRT L WHITE";"";""

回答by arutaku

In this case I would use a sedexpression instead of AWK.

在这种情况下,我将使用sed表达式而不是 AWK。

If your data is in a file called data.txt, you can get it writing:

如果您的数据在名为 的文件中data.txt,您可以将其写入:

sed "s/;/\";\"/g;s/^/\"/;s/$/\"/" data.txt

That will print the result to the std output, but if you want to replace the content of the file just use sed -ithis way:

这会将结果打印到 std 输出,但如果您想替换文件的内容,只需使用sed -i以下方式:

sed -i "s/;/\";\"/g;s/^/\"/;s/$/\"/" data.txt

And that is all !!

这就是全部!!

Explanation:The sedexpression consists in three sedcommands separated by ";" that you can run separately:

说明:sed表达式由三个sed以“;”分隔的命令组成 您可以单独运行:

sed "s/;/\";\"/g

It makes a substitution (that is what means the first "s"), then the "/" (the default separator), ";" that is what we want to replace. Then the second separator "/", and the replacement: \";\"It is a sequence: escaped quote, a semicolon and a escaped quote. So with this command we will replace semicolons ;by ";". The last /gmeans that each ;will be replaced (not only the first smicolon).

它进行替换(即第一个“s”的意思),然后是“/”(默认分隔符)、“;” 这就是我们想要取代的。然后是第二个分隔符“/”,并替换:\";\"它是一个序列:转义引号、分号和转义引号。所以用这个命令,我们将取代分号;";"。最后一个/g意味着每个都;将被替换(不仅是第一个 smicolon)。

If the input was a;b;cafter this running the first command it will be a";"b";"c.

如果输入是a;b;c在此之后运行第一个命令,它将是a";"b";"c.

Now we need to add quotes in the beginning (^in a regular expression) and in the end ($). So that is what it means:

现在我们需要在开头(^在正则表达式中)和结尾($)添加引号。所以这就是它的意思:

sed "s/^/\"/" // the first quote

And

sed "s/$/\"/" // the last quote

Getting the desired output:

获得所需的输出:

"a";"b";"c"

回答by jaypal singh

awk '{for (i=1;i<=NF;i++) $i="\""$i"\""}1' FS=";" OFS=";" input

回答by glenn Hymanman

Let me refactor your program a bit:

让我稍微重构一下你的程序:

/#!/bin/awk -f
BEGIN {
    FS=";";
    OFS="\";\"";
    IMAGE="imagepathfail";
    URLKEY="urlkeyfail";
    URLPATH="urlpathfail";
    # DEFINE CATEGORY
    CATEGORY["1"] ="2,3,4";
    CATEGORY["2"] ="2,3,7";
    CATEGORY["3"] ="2,3,8";
    CATEGORY["4"] ="2,3,22,23";
    CATEGORY["5"] ="2,3,5";
    CATEGORY["7"] ="2,3,6";
    CATEGORY["8"] ="2,27,28";
    CATEGORY["9"] ="2,27,29";
    CATEGORY["10"]="2,9,13";
    CATEGORY["11"]="2,9,14";
    CATEGORY["12"]="2,35,38";
    CATEGORY["13"]="2,9,16";
    CATEGORY["14"]="2,35,37";
    CATEGORY["15"]="2,19,21";
    CATEGORY["16"]="2,9,15";
    CATEGORY["17"]="2,9,16";
    CATEGORY["18"]="2,19,20";
    CATEGORY["20"]="2,27,33";
    CATEGORY["21"]="2,9,17";
    CATEGORY["22"]="2,3,22,24";
    CATEGORY["23"]="2,3,22,25";
    CATEGORY["24"]="2,9,13";
    CATEGORY["25"]="2,40,43";
    CATEGORY["26"]="2,40,44";
    CATEGORY["27"]="2,3,22,26";
    CATEGORY["28"]="2,27,31";
    CATEGORY["29"]="2,27,32";
    CATEGORY["30"]="2,27,30";
    CATEGORY["31"]="2,27,34";
    CATEGORY["32"]="2,9,11";
    CATEGORY["33"]="2,35,36";
    CATEGORY["34"]="2,9,10";
    CATEGORY["35"]="2,9,18";
    CATEGORY["36"]="2,40,42";
    CATEGORY["38"]="2,40,41";
    CATEGORY["39"]="2,40,45";
    CATEGORY["40"]="2,35,39";
    CATEGORY["41"]="2,9,12";
    CATEGORY["43"]="2,9,12";
    CATEGORY["01"]="2,3,4";
    CATEGORY["02"]="2,3,7";
    CATEGORY["03"]="2,3,8";
    CATEGORY["04"]="2,3,22,23";
    CATEGORY["05"]="2,3,5";
    CATEGORY["07"]="2,3,6";
    CATEGORY["08"]="2,27,28";
    CATEGORY["09"]="2,27,29";
    # header
    print "store", "websites", "attribute_set", "type", "category_ids", "sku", "has_options", "name", "image", "small_image", "thumbnail", "url_key", "url_path", "page_layout", "options_container", "msrp_enabled", "msrp_display_actual_price_type", "gift_message_available", "rsr_pn", "manufacturer_pn", "price", "cost", "weight", "msrp", "manufacturer", "status", "is_recurring", "visibility", "enable_googlecheckout", "tax_class_id", "description", "short_description", "qty", "min_qty", "use_config_min_qty", "is_qty_decimal", "backorders", "use_config_backorders", "min_sale_qty", "use_config_min_sale_qty", "max_sale_qty", "use_config_max_sale_qty", "is_in_stock", "notify_stock_qty", "use_config_notify_stock_qty", "manage_stock", "use_config_manage_stock", "stock_status_changed_auto", "use_config_qty_increments", "qty_increments", "use_config_enable_qty_inc", "enable_qty_increments", "is_decimal_divided", "stock_status_changed_automatically", "use_config_enable_qty_increments", "product_name", "store_id", "product_type_id";
}

function getCategory(val) {
    return (val in CATEGORY) ? CATEGORY[val] : "47";
}

{
    # DEFINE IMAGE WITH PATH.
    IMAGE="/5/1/""_1.jpg";
    # DEFINE URL KEY
    URLKEY=;
    # DEFINE URL PATH
    URLPATH=".html";
    print "\" "admin", "base", "Default", "simple", getCategory(), , "0", , IMAGE, IMAGE, IMAGE, URLKEY, URLPATH, "No layout updates", "Block after Info Column", "Use config", "Use config", "No", , , , , , , , "Enabled", "No", "Catalog, Search", "Yes", "Taxable Goods", , , , "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "0", "0", "1", , "0", "simple" "\"";
}