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
}