添加双引号的awk脚本输出域? [英] Add double quotes around fields in AWK script output?
问题描述
我写了一个分销商平面文件转换成CSV导入的Magento的到一个awk脚本。此文件是分号分隔。
这是不把引号每个字段,如进口方要求。它的工作原理相当不错,但造成的数据导入的一些问题,而封闭双引号。我花了几个小时,试图弄清楚如何将它添加到现有的脚本中,没有多少运气。任何帮助将大大AP preciated - 我是pretty新AWK
电流输出
<$p$p><$c$c>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管理;基础;默认;操作简单; 2,35,36; 844802016148; 0; 5.11 HOLSTER恤大号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布局更新;阻止信息栏后使用;;配置;使用配置;无; 511-40011-010-L; 40011; 74.99 ;; 48.00; 5; 74.99;启用;目录,搜索; 5.11战术;是;应税货物;不,5.11战术短袖T恤L白色皮套恤船员40011; 5.11战术短袖T恤L白色皮套恤船员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恤L白色; 0;简单;;;;;;;;;;; ;;;
所需的输出
<$p$p><$c$c>\"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\"管理员,基地;默认,简单,2,35,36,844802016148;0;5.11 HOLSTER衬衫大号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布局更新,阻止信息后列;;使用配置;使用配置,否;511-40011-010-L,40011,74.99;; 48.00,5,74.99;已启用,产品目录,搜索;5.11战术;是;应税物品;否;5.11战术短袖T恤L白色皮套恤船员40011;5.11战术短袖T恤L白色皮套恤船员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衬衫L白色;0,简单,,,,,,,,,,,,; ;
脚本 - rsrimport.awk
#!/斌/的awk -f
#------------------------------------------------- ---------------------------------------
#版权所有(C)2012年至2013年约翰·Steensen&LT; john.steensen@live.com>
# 版权所有。没有担保,明示或暗示的,提供。
#------------------------------------------------- ---------------------------------------
#AWK处理
#更新03DEC2012 @ 1552 MST
#------------------------------------------------- ---------------------------------------
#警告/注意事项扶养
#AWK
#------------------------------------------------- ---------------------------------------
开始 {
FS =;;
OFS =;;
类别=47;
IMAGE =imagepathfail;
URLKEY =urlkeyfail;
URLPATH =urlpathfail;
打印存储,网站,attribute_set,型,category_ids,SKU,has_options,姓名,图像,small_image,缩略图,url_key, url_path,page_layout,options_container,msrp_enabled,msrp_display_actual_price_type,gift_message_available,rsr_pn,manufacturer_pn,价格,成本,重,MSRP,制造商 状态,is_recurring,知名度,enable_googlecheckout,tax_class_id,说明,SHORT_DESCRIPTION,数量,min_qty,use_config_min_qty,is_qty_decimal,缺货, 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,这里给,STORE_ID,product_type_id;
}
{
#定义类别
如果($ 4个==1)之类=2,3,4;
否则,如果($ 4个==2)类别=2,3,7;
否则,如果($ 4个==3)类别=2,3,8;
否则,如果($ 4 ==4)类别=2,3,22,23;
否则,如果($ 4个==5)之类=2,3,5;
否则,如果($ 4个==7)类别=2,3,6;
否则,如果($ 4个==8)之类=2,27,28;
否则,如果($ == 4个9)类别=2,27,29;
否则,如果($ 4个==10)类别=2,9,13;
否则,如果($ 4个==11)类别=2,9,14;
否则,如果($ 4个==12)类别=2,35,38;
否则,如果($ 4个==13)类别=2,9,16;
否则,如果($ 4个==14)类别=2,35,37;
否则,如果($ 4个==15)类别=2,19,21;
否则,如果($ 4个==16)类别=2,9,15;
否则,如果($ 4个==17)类别=2,9,16;
否则,如果($ 4个==18)类别=2,19,20;
否则,如果($ 4个==20)类别=2,27,33;
否则,如果($ 4个==21)类别=2,9,17;
否则,如果($ 4个==22)类别=2,3,22,24;
否则,如果($ 4个==23)类别=2,3,22,25;
否则,如果($ 4个==24)类别=2,9,13;
否则,如果($ 4个==25)类别=2,40,43;
否则,如果($ 4个==26)类别=2,40,44;
否则,如果($ 4个==27)类别=2,3,22,26;
否则,如果($ 4个==28)类别=2,27,31;
否则,如果($ 4个==29)类别=2,27,32;
否则,如果($ 4个==30)类别=2,27,30;
否则,如果($ 4个==31)类别=2,27,34;
否则,如果($ 4个==32)类别=2,9,11;
否则,如果($ 4个==33)类别=2,35,36;
否则,如果($ 4个==34)类别=2,9,10;
否则,如果($ 4个==35)类别=2,9,18;
否则,如果($ 4个==36)类别=2,40,42;
否则,如果($ 4个==38)类别=2,40,41;
否则,如果($ 4个==39)类别=2,40,45;
否则,如果($ 4个==40)类别=2,35,39;
否则,如果($ 4个==41)类别=2,9,12;
否则,如果($ 4个==43)类别=2,9,12;
否则,如果($ 4个==01)类别=2,3,4;
否则,如果($ 4个==02)类别=2,3,7;
否则,如果($ 4个==03)类别=2,3,8;
否则,如果($ 4个==04)类别=2,3,22,23;
否则,如果($ 4个==05)类别=2,3,5;
否则,如果($ 4个==07)类别=2,3,6;
否则,如果($ 4个==08)类别=2,27,28;
否则,如果($ 4个==09)类别=2,27,29;
其他类别=47;
#定义图像的路径。
IMAGE =/ 5/1 /$ 1_1.jpg;
#定义URL KEY
URLKEY = $ 1;
#定义URL PATH
URLPATH = $ 1。html的;
打印管理员,基地,默认,简单,类别,$ 1,0,$ 3图像,图像,图像,URLKEY,URLPATH,无布局更新,阻止信息栏后 使用配置,使用配置,否,$ 1,$ 12 $ 6 $ 7,$ 8 $ 6,$ 11已启用,否,产品目录,搜索,是,应税货物 ,$ 14 $ 14 $ 90,1,0,0,1,1,1,0,1,1,0中,1,0,1,0,1,0,1,0,0,0,1,$ 3,0 ,简单;
}
结束 {}
如果你想要的:
这添加到现有的脚本。
块引用>您可以插入其他
\\在
:打印
这样的每一个参数\\
打印\\管理员\\,\\基地\\,...
编辑:
是的,也许塞汀OFS是更好的解决方案:
BEGIN {OFS =\\; \\; } ...打印\\管理,...,简单的\\;
I have written an awk script that converts a distributor flatfile into a CSV importable into Magento. This file is semi-colon delimited.
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.
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
#!/bin/awk -f # ---------------------------------------------------------------------------------------- # Copyright (c) 2012 - 2013 John Steensen <john.steensen@live.com> # 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 ($4=="1") CATEGORY="2,3,4"; else if ($4=="2") CATEGORY="2,3,7"; else if ($4=="3") CATEGORY="2,3,8"; else if ($4=="4") CATEGORY="2,3,22,23"; else if ($4=="5") CATEGORY="2,3,5"; else if ($4=="7") CATEGORY="2,3,6"; else if ($4=="8") CATEGORY="2,27,28"; else if ($4=="9") CATEGORY="2,27,29"; else if ($4=="10") CATEGORY="2,9,13"; else if ($4=="11") CATEGORY="2,9,14"; else if ($4=="12") CATEGORY="2,35,38"; else if ($4=="13") CATEGORY="2,9,16"; else if ($4=="14") CATEGORY="2,35,37"; else if ($4=="15") CATEGORY="2,19,21"; else if ($4=="16") CATEGORY="2,9,15"; else if ($4=="17") CATEGORY="2,9,16"; else if ($4=="18") CATEGORY="2,19,20"; else if ($4=="20") CATEGORY="2,27,33"; else if ($4=="21") CATEGORY="2,9,17"; else if ($4=="22") CATEGORY="2,3,22,24"; else if ($4=="23") CATEGORY="2,3,22,25"; else if ($4=="24") CATEGORY="2,9,13"; else if ($4=="25") CATEGORY="2,40,43"; else if ($4=="26") CATEGORY="2,40,44"; else if ($4=="27") CATEGORY="2,3,22,26"; else if ($4=="28") CATEGORY="2,27,31"; else if ($4=="29") CATEGORY="2,27,32"; else if ($4=="30") CATEGORY="2,27,30"; else if ($4=="31") CATEGORY="2,27,34"; else if ($4=="32") CATEGORY="2,9,11"; else if ($4=="33") CATEGORY="2,35,36"; else if ($4=="34") CATEGORY="2,9,10"; else if ($4=="35") CATEGORY="2,9,18"; else if ($4=="36") CATEGORY="2,40,42"; else if ($4=="38") CATEGORY="2,40,41"; else if ($4=="39") CATEGORY="2,40,45"; else if ($4=="40") CATEGORY="2,35,39"; else if ($4=="41") CATEGORY="2,9,12"; else if ($4=="43") CATEGORY="2,9,12"; else if ($4=="01") CATEGORY="2,3,4"; else if ($4=="02") CATEGORY="2,3,7"; else if ($4=="03") CATEGORY="2,3,8"; else if ($4=="04") CATEGORY="2,3,22,23"; else if ($4=="05") CATEGORY="2,3,5"; else if ($4=="07") CATEGORY="2,3,6"; else if ($4=="08") CATEGORY="2,27,28"; else if ($4=="09") CATEGORY="2,27,29"; else CATEGORY="47"; # DEFINE IMAGE WITH PATH. IMAGE="/5/1/"$1"_1.jpg"; # DEFINE URL KEY URLKEY=$1; # DEFINE URL PATH URLPATH=$1".html"; print "admin", "base", "Default", "simple", CATEGORY, $1, "0", $3, IMAGE, IMAGE, IMAGE, URLKEY, URLPATH, "No layout updates", "Block after Info Column", "Use config", "Use config", "No", $1, $12, $6, $7, $8, $6, $11, "Enabled", "No", "Catalog, Search", "Yes", "Taxable Goods", $14, $14, $9, "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "0", "0", "1", $3, "0", "simple"; } END {}
解决方案If you want:
add this to the existing script.
You can insert additional
\"\"
in each argument of
print "\"admin\"", "\"base\"", ...
Edited:
Yes, perhaps seting OFS is better solution:
BEGIN { OFS="\";\""; } ... print "\"admin", ...., "simple\"";
这篇关于添加双引号的awk脚本输出域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!