[SOLVED] CSV file - Fetch column value which has commas - using awk
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Like @rknichols said, it wouldn't be the best solution and maybe I should write my own code which I do not intend to do right now Instead I can work around this to make it simpler.
I know.. It is bit tacky. thing is, the first awk converts the file data as expected. and the second awk, fetches only the specified line's column value. I tried incorporating both but it does not work. Can you help me with it?
./csv_split.py '1234,some text,"some text, with comma",text,alphanumeric,"again with, comma",text'
The instring converted to list:
['1234', 'some text', '"some text, with comma"', 'text', 'alphanumeric', '"again with, comma"', 'text']
Print index 2 in the list:
"some text, with comma"
./csv_split.py '111,222,"Amy said, ""Hello""",444,555,666'
The instring converted to list:
['111', '222', '"Amy said, ""Hello"""', '444', '555', '666']
Print index 2 in the list:
"Amy said, ""Hello"""
./csv_split.py '123,foo,"f(x,y)",456'
The instring converted to list:
['123', 'foo', '"f(x,y)"', '456']
Print index 2 in the list:
"f(x,y)"
The actual code (without error handling et-al, this is just a quick implementation, can be improved a lot):
Code:
#!/usr/bin/env python3
"""
Split a csv file at ',' but keep commas in quoted strings
"""
import sys
inString = sys.argv[1]
outString = ""
isQutedString = False
for i in (range(len(inString))):
if isQutedString:
if inString[i] == "," and inString[i-1] == "\"":
outString += "|"
isQutedString = False
continue
if inString[i] == "," and inString[i+1] == "\"":
isQutedString = True
outString += "|"
elif not isQutedString and inString[i] == "," and inString[i+1] != "\"":
outString += "|"
else:
outString += inString[i]
outFileList = outString.split("|")
print("The instring converted to list:")
print(outFileList)
print("Print index 2 in the list:")
print(outFileList[2])
Also, this will of course fail if there is a literal '|' sign in the actual text. One could perhaps choose an even more esoteric character such as ł or ŋ or ª or ×. But, again, not claiming this to be bullet proof.
I believe the awk I posted will deliver what you need, just have to print the line / column you want.
Of that I am certain. But I just wanted to see if I could solve this on my own (no, I don't have a life!). Since I don't know awk very well, I threw together something in Python real quick.
Your gonna hate me HMW, but here is the ruby option
Why would I ever do that!?! I think it's great to see solutions in different languages. To me, both awk and ruby are fairly incomprehensible, that's why I stick with Python. There's only so much space left in my internal hard drive (read: brain). I don't think I will ever master any of those languages.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.