Compare string and join value into one field in bash shell - linux

I'm writing a script to modify csv file
Here's my csv file:
"ID", "Subject" , "Channels", "Description"
"24" , "Bind-0.9.3" , "Linux", "BIND (Berkeley Internet Name Domain) is an implementation of the DNS (Domain Name System) protocols"
"24" , "Bind-0.9.3", "Fedora", "BIND (Berkeley Internet Name Domain) is an implementation of the DNS (Domain Name System) protocols"
"25" , "Tar-8.0.1" , "Debian", "Tar Package"
"25" , "Tar-8.0.1", "Ubuntu" , "Tar Package"
Now, I want to compare "ID" value . If they are same value, could we join "Channels" into one field
Here's expected result:
"ID", "Subject" , "Channels", "Description"
"24" , "Bind-0.9.3" , "Linux , Fedora", "BIND (Berkeley Internet Name Domain) is an implementation of the DNS (Domain Name System) protocols"
"25" , "Tar-8.0.1" , "Debian , Ubuntu", "Tar Package"
Does anyone have an idea using awk, sed or something else in my case ?
Thanks so much
Regards,

$ cat tst.awk
BEGIN { FS="[[:space:]]*,[[:space:]]*"; OFS=" , " }
NR==1 { print; next }
{
subj[$1] = $2
desc[$1] = $4
if ($1 in chans) {
chans[$1] = chans[$1] OFS $3
}
else {
chans[$1] = $3
cnt2chan[++numChans] = $1
}
}
END {
for (chanNr=1; chanNr<=numChans; chanNr++) {
chan = cnt2chan[chanNr]
gsub(/\"/,"",chans[chan])
print chan, subj[chan], "\"" chans[chan] "\"", desc[chan]
}
}
$
$ awk -f tst.awk file
"ID", "Subject" , "Channels", "Description"
"24" , "Bind-0.9.3" , "Linux , Fedora" , "BIND (Berkeley Internet Name Domain) is an implementation of the DNS (Domain Name System) protocols"
"25" , "Tar-8.0.1" , "Debian , Ubuntu" , "Tar Package"

This might work for you (GNU sed):
sed -r ':a;$!N;s/^("[0-9]*")\s*,\s*"[^"]*"\s*,\s*"([^"]*)".*\n(\1\s*,\s*"[^"]*"\s*,\s*")/\3\2,/;ta;P;D' file
Keep a running window of 2 lines in the pattern space and if the beginning of the those lines are the same combine the channels into the second line, remove the first line and repeat.
N.B. the headers will be untouched as they do not fullfill the required pattern.

Related

Bash script how to display matched words in custom order

The following is the output of the command ads2 cls create
kernel with pid 7148 (port 9011) killed
kernel with pid 9360 (port 9011) killed
probing service daemon # http://fdt-c-vm-0093.fdtech.intern:9010
starting kernel FDT-C-VM-0093 # http://fdt-c-yy-0093.ssbt.intern:9011 name=FDT-C-VM-0093 max_consec_timeouts=10 clustermode=Standard hostname=FDT-C-VM-0093 framerate=20000 schedmode=Standard rtaddr=fdt-c-vm-0093.fdtech.ssbt tickrole=Local tickmaster=local max_total_timeouts=1000
kernel FDT-C-VM-0093 running
probing service daemon # http://172.16.xx.xx:9010
starting kernel FDT-C-AGX-0004 # http://172.16.xx.xx:9011 name=FDT-C-AGX-0004 max_consec_timeouts=10 clustermode=Standard hostname=FDT-C-AGX-0004 framerate=20000 schedmode=Standard rtaddr=172.16.xx.xx tickrole=Local tickmaster=local max_total_timeouts=1000
kernel Fxx-x-xxx-xxx4 running
>>> start cluster establish ...
>>> cluster established ...
nodes {
node {
name = "FDT-C-VM-xxxx";
address = "http://fxx-x-xx-0093.xxx.intern:xxxx/";
state = "3";
}
node {
name = "xxx-x-xxx-xxx";
address = "http://1xx.16.xx.xx:9011/";
state = "3";
}
}
However, I'm trying to extract the value of name and state for each node, save them in a variable and display them in the following order:
**#example output**
Node fdt-c-agx-xxx has state 3
Node FDT-C-VM-xxx has state 3
Till now i, with the help of this so much powerful learning site, could extract the values of name and state by executing the following:
cls="$(ads2 cls create | grep '\(state\|name\) =' | cut -d '"' -f 2)"
Now if i print cls variable, i get the following:
FDT-C-VM-xxx
3
FDT-C-AGX-xxx
3
First Question:
How can i display the result like the one above in the **#example output**?
Second question
With this implementation, how can I check the value of state varaiable for both nodes in order to print something like
if node1State = 3 && node2State = 3; then
echo "Sucess"
else
echo "Failed"
Since the output seems to be json, you should really use a json parser such as jq but in the absence of jq you can use awk and combine the requirements for question one and two:
ads2 cls create | awk -F [\"] '/^>>> cluster established .../ { strt=1 } strt!=1 { next } $1 ~ "name" { cnt++;nam[cnt]=$2 } $1 ~ "state" { stat[cnt]=$2;print "Node "nam[cnt]" has state "$2 } END { if (stat[1]=="3" && stat[2]=="3") { print "Success" } else { print "Failed" } }'
Explanation:
ads2 cls create | awk -F [\"] ' # Set the field delimiter to a double quote
/^>>> cluster established .../ {
strt=1 # If the line starts with ">>> cluster established ...", set a variable strt to 1
}
strt!=1 {
next # If strt is not equal to 1, skip to the next line
}
$1 ~ "name" {
cnt++; # If the first field contains name, increment a cnt variable
nam[cnt]=$2 # Use the cnt variable as the index of an array called nam with the second field the value
}
$1 ~ "state" {
stat[cnt]=$2; # When the first field contains "state", set up another array called stat
print "Node "nam[cnt]" has state "$2 # Print the node name as well as the state
}
END {
if (stat[1]=="3" && stat[2]=="3") {
print "Success" # At the end of processing, use the array to determine whether there is a success of failure.
}
else {
print "Failed"
}
}'

Assign variable separated by comma based on user input using function

I wanted to assign variable name separated by comma based on user input using function.
I will get the user input using below script and it will call function for variable assignment
while [ "$ans" != "q" ]
do
clear
echo "Choose your subject"
echo "Press q once done"
echo " 1.Science"
echo " 2.Maths"
echo " 3.English"
...
read ans
case $ans in
1) clear
Science;;
2) clear
Maths;;
3) clear
English;;
....
esac
done
clear
subjects=""
Science()
{
subjects+="$subjects Science"
}
Maths()
{
subjects+="$subjects Maths"
}
English()
{
subjects+="$subjects English"
}
At the end I wanted to have variable subjects to have option choose by the user:
Etc:
Science,Maths
Maths,English
Science,English
English
In bash, the function definition must be placed before any calls to the function.
The line subjects="" must be placed before the while loop. Otherwise its value will get lost (will be set to empty string) on exit from the loop.
The += operator causes double concatenation in the line subjects+="$subjects Science", since the right hand side contains already the expansion of the subjects variable. Either subjects="$subjects Science", or subjects+=" Science" must have been used (the same is also true for other lines in which the += operator is used). Besides, since a comma separated list is desired, a , character must be used while concatenating strings instead of space character. For example: subjects="$subjects,Science"
So a corrected script could be like this:
#!/bin/bash
subjects=""
Science() {
subjects="$subjects,Science"
}
Maths() {
subjects="$subjects,Maths"
}
English() {
subjects="$subjects,English"
}
while [ "$ans" != "q" ]; do
clear
echo "Choose your subject"
echo "Press q once done"
echo " 1.Science"
echo " 2.Maths"
echo " 3.English"
read -r ans
case $ans in
1) Science;;
2) Maths;;
3) English;;
esac
done
subjects=${subjects:1} # to remove the leading ',' character
echo "Your selections are $subjects"
Note: I wouldn't normally use a function just to append a simple string to a variable.

To add a string before and after, if the specific string is matched between patterns using sed in bash

Question: To add a string before and after, if the specified string is matched between patterns using sed in bash ??
In the below code, I want to add /* one line above object Host "kali" { and add */ to the next line after the occurrence of } (not to the last occurrence of }).
This is my code
object Host "linux" {
import "windows"
address = "linux"
groups = ["linux"]
}
object Host "kali" {
import "linux"
address = "linux"
groups = [linux ]
}
object Host "windows" {
import "linux"
address = "linux"
groups = ["windows" ]
}
This is the expected output:
object Host "linux" {
import "windows"
address = "linux"
groups = ["linux"]
}
/*
object Host "kali" {
import "linux"
address = "linux"
groups = [linux ]
}
*/
object Host "windows" {
import "linux"
address = "linux"
groups = ["windows" ]
}
**This is what I tried**
#! /bin/bash
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[0;32m'
clear
echo -e "Enter the names to comment in config file"
cat > comment-file.txt
clear
echo -e "#################################################################################"
echo "Please wait. The names will be commented shortly............"
echo -e "#################################################################################"
echo "Dont press any button, Please hold on...."
while read -r names
do
loc=$(grep -il "object.*Host.*\"$names.*\"" /home/jo/folders/test-sc/*.txt)
if [ -z $loc ]
then
echo -e " $names$RED No Object definition found $NONE "
else
sed -i '/object Host \"$names.*\" {/ { s,^,/*\n,
: loop
/}/ {
s,$,\n*/,
p
d
}
N
b loop
}' "$loc"
echo -e " $names - $loc - $GREEN Object host defenition commented $NONE "
fi
done < comment-file.txt
echo -e "#################################################################################"
echo -e "\t\t\t\t Script completed \t\t\t\t"
echo -e "#################################################################################"
rm -rf comment-file.txt
Error:
No changes had been made in the output file which means /home/jo/folders/test-sc/*.txt
This might work for you (GNU sed):
sed -e '/object Host "kali"/{i\/*' -e ':a;n;/}/!ba;a\*/' -e '}' file
Look for a line containing object Host "kali" insert a line before it containing /*, read/print further lines until one containing } and append the line */.
After finding object Host "kali" {:
Prepend /*\n to the pattern space
If } is seen, append \n*/ to the pattern space, print and delete
Append Next line to the pattern space and loop back to step 2.
sed -e '/object Host "kali" {/ {
s,^,/*\n,
: loop
/}/ {
s,$,\n*/,
p
d
}
N
b loop
}'
.... addendum... To properly pass "$names" to be a part of the sed script, we will need to follow the quoting rules for sh ... the idea will be to embedding "$names" into the sed script, and the sed line will look like the following:
sed -i -e "/object Host \"$names\" {/ {
s,^,/*\n,
: loop
/}/ {
s,$,\n*/,
p
d
}
N
b loop
}" "$loc"

Update a specific line with data under queueFilters section only

I have file and content has like below.
------------------------------------------------
##
servers:
# Start OF VM1
- host: "VM1"
queueFilters:
include: ["*"]
channelFilters:
include: ["*"]
# End OF VM1
# Start OF VM2
- host: "VM2"
queueFilters:
include: ["*"]
channelFilters:
include: ["*"]
# End OF VM2
---------------------------------------------------
I wanted to update parameter like include under queueFilter section : ["test1","test2","test3"] only between the lines where from # Start OF VM1 to # End OF VM1
I tried with the command,
V1=VM1
V2="test1","test2","test3"
awk -F': ' '/# Start OF '$V1'/,/# End OF '$V1'/{if( $0 ~/include/ ) {if ( $2 ~ /\[\]/ ) {gsub(/\]/,"'$V2']")} else {gsub(/\]/,",'$V2']")}}}1' input.yaml
Could some one help me how to achieve this..
First, you should assign the shell variables to awk variables, rather than trying to substitute shell variables into the script.
Second, if you only want to update queueFilters, you need to match that. You can set a variable that will be used when you get to the include line.
V1=VM1
V2='"test1","test2","test3"'
awk -F': ' -v V2="$V2" '
/# Start OF '$V1'/,/# End OF '$V1'/ {
if (/queueFilters:/) {inqueue = 1}
else if ($1 ~ /include/ && inqueue) {
if ( $2 ~ /\[\]/ ) {
gsub(/\]/, V2 "]")
} else {
gsub(/\]/, "," V2 "]")
}
inqueue = 0
}
}1' input.yaml

Add double quotes around fields in AWK script output?

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 like this:
print "\"admin\"", "\"base\"", ...
Edited:
Yes, perhaps seting OFS is better solution:
BEGIN { OFS="\";\""; } ... print "\"admin", ...., "simple\"";
awk '{for (i=1;i<=NF;i++) $i="\""$i"\""}1' FS=";" OFS=";" input
To add quotes around the entries you could use a simple AWK loop:
Script - 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";"";""
In this case I would use a sed expression instead of AWK.
If your data is in a file called data.txt, you can get it writing:
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 -i this way:
sed -i "s/;/\";\"/g;s/^/\"/;s/$/\"/" data.txt
And that is all !!
Explanation:
The sed expression consists in three sed commands separated by ";" that you can run separately:
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 /g means that each ; will be replaced (not only the first smicolon).
If the input was a;b;c after this running the first command it will be 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"
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"_1.jpg";
# DEFINE URL KEY
URLKEY=$1;
# DEFINE URL PATH
URLPATH=$1".html";
print "\" "admin", "base", "Default", "simple", getCategory($4), $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" "\"";
}
In my opinion, we could use printf (formated output) and double quote is obtain with \" into format string.
e.g.
gawk 'BEGIN{print "WKT,punto";}{printf "\"LINESTRING Z (%f %f 0,%f %f 0)\",\"%d"\n",$3,$2,$4,$5,$1}' Frecce_geoloc_12-24.txt
output:
$3 $2 $4 $5 $1
"LINESTRING Z (-72.319686 -50.609328 0,-50.609309 -72.319499 0)","6582"
"LINESTRING Z (-72.319245 -50.609215 0,-50.609195 -72.319052 0)","6583"
"LINESTRING Z (-72.318799 -50.609101 0,-50.609081 -72.318607 0)","6584"
"LINESTRING Z (-72.318366 -50.608990 0,-50.608969 -72.318169 0)","6585"

Resources