0

I am trying to write a simple bash function that will remove a column from a csv file (in-place) where the inputs are the file and the column to be deleted but I am having some difficulty:

function rm_col() {
  tmpfile=$(mktemp /tmp/function-rm_col.XXXXXX)
  /bin/cp $2 $tmpfile
  rm -f $2
  cut -d, -f"$1" --complement "$tmpfile" > "$2"
}

The thing is that I need the column to be a variable and it seems like what is happening is that the redirection is never happening because it evaluates the variables first?

I found a similar question and I tried the solutions but none of them worked for me. Anyone have a better idea?

UPDATE

Here is the full file csv2dr.bash:

#!/bin/bash
set -x

rm foo.csv
cat <<EOF > foo.csv
c1,c2,c3,c4
c1,c2,c3,c4
EOF

function rm_col() {
  tmpfile=$(mktemp /tmp/function-rm_col.XXXXXX)
  /bin/cp $2 $tmpfile
  /bin/rm -f $2
  cut -d, -f"$1" --complement "$tmpfile" > "$2"
}

function csv2dr_vr() {
  tmpfile=$(mktemp /tmp/function-csv2dr_vr.XXXXXX)
  /bin/cp -f $1.csv $tmpfile
  cat $tmpfile
  rm_col 2 $tmpfile
  sleep 2
  cat $tmpfile
}

csv2dr_vr foo

set +x

Probably is some stupid programming error but I am not seeing it. Here is the full output:

+ rm foo.csv
+ cat
+ csv2dr_vr foo
++ mktemp /tmp/function-csv2dr_vr.XXXXXX
+ tmpfile=/tmp/function-csv2dr_vr.FmYSWc
+ /bin/cp -f foo.csv /tmp/function-csv2dr_vr.FmYSWc
+ cat /tmp/function-csv2dr_vr.FmYSWc
c1,c2,c3,c4
c1,c2,c3,c4
+ rm_col 2 /tmp/function-csv2dr_vr.FmYSWc
++ mktemp /tmp/function-rm_col.XXXXXX
+ tmpfile=/tmp/function-rm_col.BXQYTD
+ /bin/cp /tmp/function-csv2dr_vr.FmYSWc /tmp/function-rm_col.BXQYTD
+ /bin/rm -f /tmp/function-csv2dr_vr.FmYSWc
+ cut -d, -f2 --complement /tmp/function-rm_col.BXQYTD
+ sleep 2
+ cat /tmp/function-rm_col.BXQYTD
c1,c2,c3,c4
c1,c2,c3,c4
+ set +x

UPDATE 2

Just for completeness here is the fixed function:

function rm_col() {
  tmpfile=$(mktemp /tmp/function-rm_col.XXXXXX)
  cut -d, -f"$1" --complement "$2" > "$tmpfile"
  /bin/cp $tmpfile $2
}
Community
  • 1
  • 1
stephenmm
  • 2,640
  • 3
  • 30
  • 48
  • 1
    Missing double quote after `> "$2`. – choroba Oct 05 '16 at 23:39
  • There's no problem redirecting to a filename in a variable. The other question is totally different because it put the `>` operator in the variable as well. – Barmar Oct 06 '16 at 00:00
  • Is the missing quote in the real code, or just a copying error? If the latter, please edit the question to fix it. Better yet, cut and paste the real function so we don't have to wonder if there are other copying errors. – Barmar Oct 06 '16 at 00:02
  • Sorry for the typo. Fixed. – stephenmm Oct 06 '16 at 00:09
  • 1
    Typically, it's a better idea to write the new data to the temp file, then replace the original with the temp file, so there is no "gap" in availability of the original. – chepner Oct 06 '16 at 01:06
  • @chepner that was it! Thanks. Make it an answer and I'll accept. – stephenmm Oct 06 '16 at 01:11

0 Answers0