pra*_*upd 9 bash text-processing html csv
我有一个Medical.csv包含以下格式行的文件,
field: 'participation.type', displayName: 'program_type', type: 'String',path:'participation'
field: 'participation.program', displayName: 'program_name', type: 'String',path:'participation'
Run Code Online (Sandbox Code Playgroud)
我想编写一个bash 脚本,将其转换为带有field,displayName和type动态标题的HTML 表。
将Csv2HtmlConverter.sh(通过在回答灵感转换CSV HTML表格使用到)是
echo "<table>" ;
while read INPUT ; do
echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" ;
done < Medical.csv ;
echo "</table>"
Run Code Online (Sandbox Code Playgroud)
对于上面的脚本的结果如下这是罚款,在一定程度上,但我想补充<th>field</th>,<th>displayName</th>动态。
<table>
<tr><td>field: 'participation.type'</td><td> displayName: 'program_type'</td><td> type: 'String'</td><td>path:'participation'</td></tr>
<tr><td>field: 'participation.program'</td><td> displayName: 'program_name'</td><td> type: 'String'</td><td>path:'participation'</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
rzy*_*mek 13
这将解决问题:
echo "<table>" ;
print_header=true
while read INPUT ; do
if $print_header;then
echo "<tr><th>$INPUT" | sed -e 's/:[^,]*\(,\|$\)/<\/th><th>/g'
print_header=false
continue
fi
echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" ;
done < Medical.csv ;
echo "</table>"
Run Code Online (Sandbox Code Playgroud)
所用正则表达式的解释是sed:
:[^,]*(,|$)
Run Code Online (Sandbox Code Playgroud)

这将匹配: 'participation.type',和:'participation'\n($表示正则表达式中的输入/行结束)。