Paste output into a CSV file in bash with paste co

2019-09-14 16:02发布

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条回答
你好瞎i
2楼-- · 2019-09-14 16:41

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

查看更多
登录 后发表回答