awk question on handling *.CSV "text fields" in awk
I'm still more comfortable using sed than awk, and tried to reverse a solution I gave on another post.
Code:
cat example.csv If you have an entry like: John "Skinny" Benson, oocalc will save a csv file with ""Skinny"" in the text field, so I modified the awk command a bit to handle this situation. |
with your sample,
Code:
# awk -F"," '{gsub(/"/,"",$1);print $1,$2,$3}' file Code:
"Jim , Jackson",100,10/12/04 |
No, the first field contains a name. This is a contrived example but I don't think that a persons name in a spread sheet column is something that would be very odd.
It occurred to me that such a table might contain nicknames which would naturally be enclosed in quotes, so I entered three lines in oocalc and saved it as a .csv file: Code:
cat example2.csv Quote:
For the answer to someone elses post, I included an awk command to translate from this type of file structure to a csv file. After testing it, I thought that doing the opposite would be a good learning exercise. You solution doesn't work for the third entry. However it did show me how to modify it to do things differently: Code:
awk -F, 'BEGIN {OFS="\n"; ORS="\n\n"} $1 ~ /^".*"$/ {gsub(/^"/,"",$1);gsub(/"$/,"",$1);gsub(/""/,"\"",$1); print $1"\n"$2"\n"$3 }' example2.csv Having to assign one of the fields so that the OFS and ORS variables would be updated was something I hadn't realized before today. compare: Code:
> echo -e 'ab cd ef\ngh ij kl\nmn op qr\n' | awk 'BEGIN { OFS="," } {print }' |
no problem. i didn't help much either. just so you might want to know, gsub() takes in alternation (regexp) so similar subs can be shortened.
Code:
awk '{ gsub(/pat1|pat2|pat3/,"replacement" ) }' file |
Your code doesn't won't work when any field other then the first is quoted, and you have to know the number of fields for it to work. This will get around both these problems, however its harder to work with this since it doesn't really split the file into meaningful records/fields.
Code:
gawk -F'""' '{gsub(/"/,"",$1);print $0(RT=="\n"?"\n":"")}' OFS='"' RS='"*,"*|\n' example.csv Code:
gawk -F'"*,"*|^"' '{gsub(/""/,"\"",$2);print $2,$3,$4}' OFS='\n' ORS='\n\n' example.csv |
Thank you for the generalized solution. Yes, I am looking at this to learn more about AWK. I haven't gotten around to learning the perl language yet. When I use a flat file to store records, I use a tab separated file. The only real example I have is storing file backup information at work and then producing a pdf catalog from these files. This is using cygwin on Win2000.
|
jschiwal,
Can you please contact me via e-Mail? Ta, Tink |
Quote:
john 3 5 4 Mary 7 8 4 Bob 2 6 4 i need to add the integers individually so the output should be john 12 Mary 19 Bob 12 i have a command that adds all of them but i need to get sum before it gets a character. awk '{s+=$1} END {print s}' filename Please help? |
Why not raise your own question instead of hijacking an ancient one???
|
All times are GMT -5. The time now is 11:39 PM. |