[英]Chunk txt file into an html table with bash
I have a ~600 line .txt file, all.txt. 我有一个〜600行的.txt文件all.txt。 I want to turn it into an HTML table with two columns where the first column contains the entire contents of all.txt with exactly five lines in each row (so the table would have about 120 rows total). 我想将其转换为具有两列的HTML表,其中第一列包含all.txt的全部内容,每行中恰好有五行(因此该表总共约有120行)。 Here's what I have so far: 这是我到目前为止的内容:
#!/bin/bash
A=10
B=5
len=`wc -l < all.txt`
len2=$((len / 5))
while [ $A -le $len2 ]; do
head -$A all.txt | tail -$B |
awk '
BEGIN{print " "}
{printf("<tr><td></td><td></td></tr>\n")}
END{print " "}
'
let A+=5
done
As you can see, this is really just a shell of a script, and I'm not sure what to with awk inside the loop. 如您所见,这实际上只是脚本的外壳,而且我不确定在循环内使用awk怎么办。 Is using awk inside a control structure like this even the best way to do it? 在这样的控制结构内部使用awk甚至是最好的方法吗? Is bash even a reasonable way to do it? bash甚至是合理的方式吗?
sed
alternative sed
替代
$ sed -e '5~5a</td>\n<td></td>\n</tr>' -e '1~5i<tr>\n<td>' <(seq 15)
<tr>
<td>
1
2
3
4
5
</td>
<td></td>
</tr>
<tr>
<td>
6
7
8
9
10
</td>
<td></td>
</tr>
<tr>
<td>
11
12
13
14
15
</td>
<td></td>
</tr>
You properly only need awk: 您只需要awk即可:
awk 'NR%5 == 1{printf "%s<tr><td>", NR > 1 ? "</td><td></td></tr>" : ""}1' all.txt
Might need to add END{print "</td><td></td></tr>"}
at the end. 可能需要在末尾添加END{print "</td><td></td></tr>"}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.