-2

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.

5
  • You mean like 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 plain cut.) Commented Jan 31, 2024 at 7:12
  • 3
    Please provide example input and expected output. Use code blocks to format them,. Commented Jan 31, 2024 at 7:22
  • The output should be that all the data which is present in the CSV file is now displayed on unix without using the actual print support format. Like just the data on the scereen but with loop command Commented Jan 31, 2024 at 9:02
  • 1
    That's fine, and not what I asked. Show us example input and and what output you expect for that example input. Commented Jan 31, 2024 at 9:23
  • Try to read your question from the point of view of someone trying to help you. It's like if you were asking a mechanic to help you fix your car but only told them where the car is parked (somewhere they can't get to!) and didn't show them the actual car. We can't reliably help you parse a file if you don't show us representative contents of the file, without that we're just guessing. Commented Feb 5, 2024 at 15:24

1 Answer 1

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.