Using csvtool call to filter csv in bash

Viewed 1870

I'm trying to filter a csv by a value in a specific column. My script currently looks like this:

function csv_grep {
  if [ $1 == "$SEARCH_TERM" ]
  then
    echo "$2"
  fi
}

export -f csv_grep

VALUES=$(csvtool namedcol col1,col2 dictionary.csv | csvtool call csv_grep -)

However when I run it, I get

/bin/bash: csv_grep: command not found
csv_grep: terminated with exit code 127

I've got version 1.4.2-1 installed so this bug report should not apply.

Any ideas what I'm doing wrong or better approaches to the task at hand?

1 Answers

I've got version 1.4.2-1 installed so this bug report should not apply.

Actually, it looks like you have hit exactly the problem described in that bug report, as we can verify with a simple test. Here's my test environment:

# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"

# dpkg -l csvtool
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
ii  csvtool        1.4.2-1      amd64        handy command line tool for handl

Let's create and export a function:

testfunction() {
  echo column one is "$1"
}

export -f testfunction

And verify that it was successfully exported:

$ bash -c 'testfunction one'
column one is one

Now let's try calling it with csvtool:

$ echo one,two,three | csvtool call testfunction -
/bin/bash: testfunction: command not found
testfunction: terminated with exit code 127

With the bug report in mind, let's take a look at /bin/sh:

$ ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Oct  3 09:58 /bin/sh -> dash

So /bin/sh is not /bin/bash. Let's fix that:

$ sudo ln -sf /bin/bash /bin/sh
$ ls -l /bin/sh
lrwxrwxrwx. 1 root root 9 Oct  3 10:05 /bin/sh -> /bin/bash

And try csvtool again:

$ echo one,two,three | csvtool call testfunction -
column one is one

Any ideas what I'm doing wrong or better approaches to the task at hand?

I would never try to process csv files in a shell script. I would probably reach for Python and the csv module.

Related