0

I have a csv file like this :

name,username
name2,username2
etc...

And I need to extract each column into lists so I can create a account (admin script).
I am hoping the result would look like this :

NAMES=( name name2 )
MAILS=( username username2 )
LENGHT=3 # number of lines in csv files actually

I would like to do it in python (because I use it elsewhere in my script and would like to convert my collegues to the dark side). Exept that I am not really a python user...
Something like this would do the trick (I assume) :

NAMES=( $(echo "$csv" | pythonFooMagic) )
MAILS=( $(echo "$csv" | python -c "import sys,csv; pythonFooMagic2") )
LENGHT=$(echo "$csv" | pythonFooMagic3)

I kind of found tutos to do it accross several lines but glued together it was ugly.
There must be some cool ways to do it. Else I will resign to use sed... Any ideas?

EDIT : ok bad idea, for future reference, see the comments

4
  • 6
    Using another scripting language to produce separate shell variables isn't exactly efficient or straightforward. Why not just use Python for the whole job and forgo shell altogether?
    – Martijn Pieters
    Feb 11, 2014 at 10:52
  • Yep I understand that, but I am confined to bash as prerequisite. I could well use cut and tail to extract the csv but it is no fun.
    – tastadmoon
    Feb 11, 2014 at 10:54
  • Using the other tool may be not fun, but making Python run 3 times (not exactly the lightest process) for just no good reason is even worse. Either stick to standard command line tools or entirely to Python. That said... Feb 11, 2014 at 11:05
  • Output of python would be to print something like this : "name name2" and for the second line "username username2".
    – tastadmoon
    Feb 11, 2014 at 11:05

1 Answer 1

0

You could use a temporary file, like this:

tmpfile=$(mktemp)
# Python script produces the variables you want
pythonFooMagic < csv > $tmpfile
# Here you take the variables somehow. For example...
source $tempfile
rm $tmpfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.