Paste output into a CSV file in bash with paste command

巧了我就是萌 提交于 2019-12-13 07:48:32

问题


I am using the paste command as below:

message+=$(paste <(echo "${arr[0]}") <(echo "$starttimeF") <(echo "$ttime") <(echo "$time") | column -t)

echo "$message"

It gives me the following output:

ASPPPLD121  11:45:00  00:00:16  00:02:23
FPASDDF123  11:45:00  00:00:16  00:02:23
ZWASD77F0D  09:04:58  02:40:18  03:51:10
DDPADSDSD5  11:29:41  00:15:35  01:17:33

How do I redirect this to a CSV or an EXCEL FILE?

OR

How do I put it into HTML table?


回答1:


You have three questions in one. The easiest to solve is the CSV file output:

echo $message | awk 'BEGIN{OFS=","}{$1=$1}1' > mycsvfile.csv

This will also open in excel since excel can read csv files, so maybe that solves the second question?

Awk is opening the file and reading line by line. We set the OFS (Output Field Seperator) to a comma. Then we just set one of the fields equal to itself, which is a just a trick to get awk to process the record, and let it print out the results to the CSV file.

I suppose you could use awk to print out a HTML table too, but it seems like a gross way of doing things:

echo $message | awk 'BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}'

html output awk script stolen from here



来源:https://stackoverflow.com/questions/43524670/paste-output-into-a-csv-file-in-bash-with-paste-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!