All,
I have an issue, Any input from your experience is highly regarded.
I have a .csv file ( in which some data inside the fields is also seperated by a comma ), I need to get a pipedelimited falt file,
So If I am doing a , It's substituting all comma "," with a delimter "|"
BEGIN{}
{
if(NR != 1)
{
gsub(",","|")
}
}
END {}
So on a UNIX box when you see the .csv file, I need to replace only the comma (",") between the fields, not the one inside the double quotes.
Name, Age, Location,Phone
RK, 29, "LAS VEGAS,Nevada",410-xxx-xxxxx^M
KB, 31, "PHeonix, Arizona",602-xxx-xxxx^M
As of Now I have designed an awk script which read this csv file and spits out what I need, the output looks like this,
Name| Age| Location|Phone
RK| 29| "LAS VEGAS,Nevada"|410-xxx-xxxxx^M
KB| 31| "PHeonix, Arizona"|602-xxx-xxxx^M
But this script has a limitation, in the case it works on only those csv files which can have ony one field that can have comma seperated data, In other words, if the csv file looks like this, It will not work
Name, Age, Location,Phone
"RK,Rivaldo", 29, "LAS VEGAS,Nevada",410-xxx-xxxxx^M
I am looking to generalize the script, nomatter what it should replace only those commas "," that seperate the fields, no the ones inside the double quotes
The script I am using now is
BEGIN{}
{
gsub(",","|")
gsub("^M","")
if(NR != 1){
value = index($0,"\"")
if(value > 0){
before_val = substr($0,1,value)
after_val = substr($0,value+1)
value2 = index(after_val,"\"")
subs_val = substr(after_val,1,value2)
subs_val_2 = substr(after_val,value2+1)
gsub(/\|/,",",subs_val)
#sub(/\|/,",",after_val)
total_val = before_val subs_val subs_val_2
gsub(/\"/," ",total_val)
print total_val > output_file
}
else {
gsub(/\"/," ")
print $0>>output_file
}
}
}
END{}
Any input on this Highly apprecitaed,
Thank you for all your time
R9ronaldo
I have an issue, Any input from your experience is highly regarded.
I have a .csv file ( in which some data inside the fields is also seperated by a comma ), I need to get a pipedelimited falt file,
So If I am doing a , It's substituting all comma "," with a delimter "|"
BEGIN{}
{
if(NR != 1)
{
gsub(",","|")
}
}
END {}
So on a UNIX box when you see the .csv file, I need to replace only the comma (",") between the fields, not the one inside the double quotes.
Name, Age, Location,Phone
RK, 29, "LAS VEGAS,Nevada",410-xxx-xxxxx^M
KB, 31, "PHeonix, Arizona",602-xxx-xxxx^M
As of Now I have designed an awk script which read this csv file and spits out what I need, the output looks like this,
Name| Age| Location|Phone
RK| 29| "LAS VEGAS,Nevada"|410-xxx-xxxxx^M
KB| 31| "PHeonix, Arizona"|602-xxx-xxxx^M
But this script has a limitation, in the case it works on only those csv files which can have ony one field that can have comma seperated data, In other words, if the csv file looks like this, It will not work
Name, Age, Location,Phone
"RK,Rivaldo", 29, "LAS VEGAS,Nevada",410-xxx-xxxxx^M
I am looking to generalize the script, nomatter what it should replace only those commas "," that seperate the fields, no the ones inside the double quotes
The script I am using now is
BEGIN{}
{
gsub(",","|")
gsub("^M","")
if(NR != 1){
value = index($0,"\"")
if(value > 0){
before_val = substr($0,1,value)
after_val = substr($0,value+1)
value2 = index(after_val,"\"")
subs_val = substr(after_val,1,value2)
subs_val_2 = substr(after_val,value2+1)
gsub(/\|/,",",subs_val)
#sub(/\|/,",",after_val)
total_val = before_val subs_val subs_val_2
gsub(/\"/," ",total_val)
print total_val > output_file
}
else {
gsub(/\"/," ")
print $0>>output_file
}
}
}
END{}
Any input on this Highly apprecitaed,
Thank you for all your time
R9ronaldo