-1

我正在尝试找到一种在新行中显示 csv 中标题输出的方法。

>head -n 1 sample.csv
col1,col2,col3

期待 :-

col1
col2
col3
4

2 回答 2

0

您可以尝试如下

代码:

line=$(head -n 1 filename.csv) #get first line of a file
final=$(echo $line | sed 's/,/\\n/g') #replace "," with "\n"
echo -e $final #output with "\n" as new line

单线:

echo -e $(head -n 1 col.csv | sed 's/,/\\n/g')

例子:

bash-3.2$ cat col.csv
Column1,Column2,Column3,Column4,Column5
abc,123,qwe,789,oiu
adasd,4356,asd,324,dsfs
asd,,,,bash-3.2$ 
bash-3.2$ 
bash-3.2$ line=$(head -n 1 col.csv)
bash-3.2$ 
bash-3.2$ final=$(echo $line | sed 's/,/\\n/g')
bash-3.2$ 
bash-3.2$ echo -e $final
Column1
Column2
Column3
Column4
Column5
bash-3.2$ 

祝你好运 !!

于 2019-11-28T20:05:53.080 回答
0

我能做的最简单的事情:

$ head -n 1 sample.csv | tr ',' '\n'
col1
col2
col3
于 2019-11-29T01:14:49.763 回答