Reputation: 1
I am using Jupyter Notebook. I need to run an awk command in the cell to check a csv file to make sure that every record has the same number of columns. I import the csv file:
fname = 'abc.csv'
then when I run the following awk command in the cell:
*!awk -F',' '{print NF; exit}' abc.csv*
it works.
However, if I wish to run it for multiple files through a variable fname
as follows:
*!awk -F',' '{print NF; exit}' fname*
it gives me the following error:
awk: fatal: cannot open file `fname' for reading (No such file or directory)
I have also tried:
*!awk -F',' '{print NF; exit}' $fname*
in which case it just keeps running. But, according to the script, it is supposed to exit after reading the first record itself (and it does so in case of running with abc.csv). Since I need to run it with multiple files, I need the awk command to take the fname
as input.
What am I doing wrong?
Thanks.
Upvotes: 0
Views: 437
Reputation: 61
you can't share variables between python and bash in this way
when you are using !awk
you call to shell interpreter that call to awk
interpreter
if you want to share a variable you need to export the variable as an environment variable
from os import environ
fname = 'abc.csv'
environ['fname'] = fname
and then you can pass the variable to awk
like this
!awk -F',' '{print NF; exit}' "$fname"
Upvotes: 1