I would like to append a column to a csv file using a bash script given a condition. The condition is that the column in file1.csv must have more than one unique value to be added to newfile.csv. These are not the real files. The original file has a lot more columns/rows.
Something like this:
file1.csv
1, ah, th, ab, a
2, ah, jk, ab, b
3, ah, lk, ab, c
4, ah, hh, ab, d
newfile.csv should be:
1, th, a
2, jk, b
3, lk, c
4, hh, d
This is the script I tried. However, it does not append the new columns. The output is just a csv with the last column of file1.csv that had more than one unique value.
#!/bin/bash
cut -d, -f1 file1.csv > newfile.csv
limit=1
for i in $(seq 2 5); do
value=$(cat file1.csv | cut -d, -f$i | uniq -u | wc -l)
if [ $value -gt $limit ]; then
paste -d, newfile.csv <(cut -d, -f$i file1.csv) > newfile.csv
else echo "Column $i not appended."
fi
done
I suspect it may have something to do with the fact I have newfile.csv twice in one line. I tried creating a new file newfile2.csv for each interaction, but that did not work. I am new to Bash.