"cola","colb","colc","cold","cole","colf" "a","b","c","d","e","f" "a1","b1","c1","d1","e1","f1" "a2","b2","c2","d2","e2","f2"
假设这是CSV文件,我希望grep
列"e"
中的值"cole"
并将其存储到shell变量中。然后使用shell变量作为wget
命令的一部分。
我该怎么做?
答案 0 :(得分:1)
set -f # disable globbing
variable="$(awk 'NR==2 {print $5}' file)"
set +f
答案 1 :(得分:1)
Awk很适合这个。如果您知道列号,则可以执行以下操作:
$ awk 'NR==2{print $5}' file.csv
e
这将在第二行打印第五个字段。如果要使用列名,则:
$ awk 'NR==1{for(i=1;i<=NF;i++)c[$i]=i}NR==2{print $c[col]}' col="cole" file.csv
e
只需设置col="<name of column to use>"
。
您可以使用命令替换将值存储在变量中:
$ val="$(awk 'NR==2{print $5}' file.csv)"
$ wget --what-ever-option "$val"
或者只是使用它:
$ wget --what-ever-option "$(awk 'NR==2{print $5}' file.csv)"