I have a CSV file saved on my desktop with many columns.
The CSV file has only one row but 195 columns and I have to print each column in Unix using loop command. The path of the file is /home/mobaxterm/Desktop/final_geneID.csv. I'm a newbie and hence don't know what to do. If someone has the answer then please help.
1 Answer
Assuming the CSV data set is a header-less CSV file with the default field delimiter (comma) and quoting characters, you may use Miller (mlr, a CSV-aware tool for working with structured data) to iterate over all fields with a for loop and emit them on separate lines:
mlr --csv -N --from=file.csv put -q 'for (k,v in $*) { emit v }'
This reads the file file.csv as a header-less CSV file. For each record, it iterates over all fields and emits each one in turn.
With the following example data:
A,B,"C,D",E,"F G"
... (where the space between F and G is a literal tab character), this would output the following:
A
B
"C,D"
E
F G
Note that the output is still CSV formatted, so the double quotes around the field with the embedded comma remain. You may change --csv to --c2p to convert the output to "pretty-printed format", which does not quote fields with embedded commas.
for((i=1; i<=195; ++i)) do cut -d, -f"$i" /home/mobaxterm/Desktop/final_geneID.csv; done? What should happen with the output from the loop? Each to a different file, or concatenated on standard output (which this snippet does)? (This assumes that the CSV file has no complex quoting; then you need a more robust tool than plaincut.)