start by saying, I\'m very new to using bash and any sort of script writing in general.
I have a csv file that has basic column headers and values underneath which look
If you have a simple multi-space delimited file (as in your example) awk
is the best tool for the job. To select the column by name in awk
you can do something like:
$ awk -v col="b" 'FNR==1 { for (i=1;i<=NF;i++) if ($i==col) x=i; next }
{print FNR-1 OFS $x}' file
1 3
2 5
3 5
4 8
Like this? Using awk:
$ awk 'NR>1{print NR-1, $2}' file
1 3
2 5
3 5
4 8
Explained:
$ awk ' # using awk for the job
NR>1 { # for the records or rows after the first
print NR-1, $2 # output record number minus one and the second field or column
}' file # state the file
I would like to be able to do this for various different named column headers. With awk you don't specify the column header name but the column number, like you don't state b
but $2
.
awk 'NR>1 {print i=1+i, $2}' file
NR>1
skips the first line, in your case the header.
print
print following
i=1+i
prints i, i is first 0 and add 1, so i is 1, next time 2 and so on.
$2
prints the second column.
file
is the path to your file.