Using csvtk or csvkit, or miller or xsv.... csvtk is a cross-platform, efficient, practical and pretty CSV/TSV toolkit in Golang. Hope you enjoy it.
For a CSV file:
$ cat 3.csv
id,name,hobby
1,bar,baseball
2,bob,basketball
3,foo,football
4,wei,programming
Select column(s) by name(s):
$ csvtk cut -f name,id 3.csv
name,id
bar,1
bob,2
foo,3
wei,4
If your input file is tab-delimited, just add the option -t
or --tabs
:
# yes, it can read from stdin
$ cat 3.csv | csvtk csv2tab
id name hobby
1 bar baseball
2 bob basketball
3 foo football
4 wei programming
$ cat 3.csv | csvtk csv2tab | csvtk cut -t -f name,id
name id
bar 1
bob 2
foo 3
wei 4
It would be useful if you could provide few lines from the file.
A web search will lead you to answers e.g. this.
I think he/she wants to select by character, not by field number, so he/she needs a way to get to know what is the column number of the word he/she seeks in the header.
The name/column number can be easily found by multiple ways (head etc) without having to open the file in a text editor.
Fundamental issue with many new participants on Biostars appears to be that they do not seem to make any effort on their own to find answers for questions. Providing ready to run command lines gets the job done for posters but it may not teach them usable skills. Other mods have discussed this in other threads but it seems to be a lost cause.
@genomax2, in addition, after getting read-to-work solution, few users extend their question, for example in this thread - C: how to select with unix a column by the column name in a txt files. This is not specific to this thread but there are multiple.
Unfortunately they do so without going back and editing original question. As a result some of these threads become
chat
sessions (Having participated in some of these I can't deny responsibility).Wish it would be possible to flag (for mods or users) some of these comments as supplemental questions so future visitors can figure out where question/answer pairs are.