在新行中显示csv文件的列标题

问题描述 投票:0回答:1

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

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

期望:-

col1
col2
col3
linux bash sed
1个回答
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$ 

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.