[CALUG] Reading CSV using Python / PHP

Ken Jackson linux at kenjackson.us
Thu Apr 28 13:00:00 EDT 2016


AWK!

Here's an short example of awk with a .csv file from inside a bash script:

   awk -v locname="${FILE}" -- '
       BEGIN { FS = ",[[:space:]]*" }  # Field sep: comma, arbitrary space
       /^[[:space:]]*#/ { next }       # Skip comment lines
       $4 == locname { print $1; exit }
       ENDFILE { print locname }       # Only printed if not exited above
   ' "$DATADIR/data.csv"

I've never handled multi-line columns, but it shouldn't be two awfully hard
since all fields are surrounded by quotes.

AWK is blindingly fast.

-Ken Jackson

On 04/28/2016 10:36 AM, Rajiv Gunja wrote:
> Hey all, I am trying to cut down on the amount of manual steps I have 
> to take editing CSV file - sorting, creating new tabs and categorising 
> them.
> I googled and found logparser from MS, could not figure it out - maybe 
> a mental block towards anything MS.
>
> I saw that it was possible using python or PHP. So turning to you guys 
> for advice. Which one works best or is there a different way?
>
> I do not know Python, but I can learn. PHP, I am very familiar with, 
> but whichever is efficient and can do a better job, I will use it.
>
> Problem:
> 1. CSV file has 2000+ rows
> 2. Some columns are multi-line with \r and \n
> 3. fields are separated by "," and each data item is between quotes (" ")
>
> Please let me know.
> -GG
>
>
> _______________________________________________
> CALUG mailing list
> CALUG at unknownlamer.org
> http://lists.unknownlamer.org/listinfo/calug


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.unknownlamer.org/pipermail/calug/attachments/20160428/4bbb0c33/attachment.html>


More information about the CALUG mailing list