Add double quotes around fields in AWK script output?

Add double quotes around fields in AWK script output?

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add double quotes around fields in AWK script output?

Enclosing AWK Script Output with Double Quotes

The Challenge: Missing Quotes Around Fields

You have written an AWK script to convert a distributor flatfile into a CSV format for importing into Magento. Your script is semi-colon delimited and works well, but it’s missing one crucial element required by the importer - double quotes around each field.

Without those enclosing quotes, you’re encountering issues during the data import process. The importer expects each value to be properly quoted to handle any special characters or whitespace correctly in the fields.

Understanding the Problem

Let’s take a closer look at your current output and desired output:

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;admin;5.11 HOLSTER SHIRT L WHITE;;;Enabled;No;Catalog, Search;Yes;Taxable Goods;5.11 HOLSTER SHIRT L WHITE;0;simple

Notice how the values are not quoted, which is causing issues with special characters and whitespace in some fields.

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"
"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";"admin";"5.11 HOLSTER SHIRT L WHITE";"";"";"";"";"";"";"Enabled";"No";"Catalog, Search";"Yes";"Taxable Goods";"5.11 HOLSTER SHIRT L WHITE";"0";"simple"

Now you can see the difference - every value is properly enclosed in double quotes.

The Solution: Enclosing Values with Double Quotes

To solve this issue, we need to modify your AWK script to enclose each field value with double quotes before printing them. Here’s how you can do it:

Using the gsub() Function to Add Double Quotes

The gsub() function in AWK allows us to perform a global substitution on all fields. We’ll use this to add double quotes around every value.

Replace the line containing your print statement with the following code:

{
  # DEFINE CATEGORY...
  # ... (rest of your existing code)

  # Use gsub() to enclose each field in double quotes
  gsub(/"/, "\"\"", $0);
  print "\""$0"\"";  # Print each field enclosed in double quotes

  # ... (rest of your existing code)
}

What the Code Does

  • The gsub(/"/, "\"\"", $0) line uses the gsub() function to replace any single quote (") in the current record with two consecutive double quotes ("").
  • This ensures that each field value is properly enclosed within double quotes.
  • The final print "\""$0"\""; statement prints each field, now correctly quoted.

Conclusion

To properly handle special characters and whitespace during your data import process, it’s crucial to ensure every value in the CSV output is enclosed within double quotes.

By using the gsub() function in AWK as shown above, you can easily modify your existing script to add those necessary double quotes around each field. This will resolve the issues you’re experiencing and make your data import process smoother.

Remember that consistency in your data formatting is key when dealing with CSV files, so always ensure proper quoting of values to avoid any potential parsing issues.

Happy coding! Let me know if there’s anything else I can help with on this topic.

Checkout Catalog Themes Configuration

Our network