Linux

如何使用 shell 腳本對數據輸出進行拆分和格式化?

  • October 3, 2019

我正在嘗試將YAML文件轉換為HTML表格,它涉及多個複雜的條件,我知道這可以使用shell腳本來完成,但是我在實現過程中遇到了一些問題,所以來社區尋求幫助。

YAML 內容格式如下。

- soft1:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft1_beta_ver:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft1_alpha_ver:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft2:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft2_beta_ver:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft2_alpha_ver:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip

< Omit more... >

它記錄了多個軟體的歷史版本,我需要將其轉換為HTML表格程式碼並單獨輸出到文件中。

例如輸出soft1, soft1_beta_ver, 和soft1_alpha_ver到同一個文件(文件名使用soft1),soft2 到另一個文件。

需要轉換的HTML表格的格式如下。

<table>
   <thead>
       <tr>
           <th>type</th>
           <th>ver</th>
           <th>link</th>
       </tr>
   </thead>
   <tbody>
       <tr>
           <td>soft1</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft1</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft1</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
       <tr>
           <td>soft1_beta_ver</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft1_beta_ver</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft1_beta_ver</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
       <tr>
           <td>soft1_alpha_ver</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft1_alpha_ver</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft1_alpha_ver</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
   </tbody>
</table>

這是我正在嘗試的shell腳本,我不知道如何將輸出拆分為多個文件,以及如何獲取軟體類型的變數。

#!/usr/bin/env bash

cat  << EOF
<table>
   <thead>
       <tr>
           <th>type</th>
           <th>ver</th>
           <th>link</th>
       </tr>
   </thead>
   <tbody>
EOF

while IFS=": " read -r softver softlink
do
cat << EOF
       <tr>
           <td>$softver</td>
           <td></td>
           <td><a href="$softlink">download</a></td>
       </tr>
EOF
done

cat << EOF
   </tbody>
</table>
EOF

對此的任何幫助或建議將非常有幫助並非常感謝。

只需從原始輸入文件生成您想要的 HTML:

$ cat ../tst.awk
/^-/ {
   sub(/:$/,"")
   out = type = $NF
   sub(/_.*/,"",out)
   close(out)
   if ( !seen[out]++ ) {
       prtBeg()
   }
   next
}
{
   sub(/:$/,"",$1)
   prtElt("<tr>")
   prtElt("<td>" type "</td>")
   prtElt("<td>" $1 "</td>")
   prtElt("<td>" $2 "</td>")
   prtElt("</tr>")
}
END {
   for (out in seen) {
       prtEnd()
   }
}

function prtElt(str) {
   depth[out] += gsub("<[^/<>]+>","&",str)
   printf "%*s%s\n", (depth[out]-1)*4, "", str >> out
   depth[out] -= gsub("</[^<>]+>","&",str)
}

function prtBeg() {
   prtElt("<table>")
   prtElt("<thead>")
   prtElt("<tr>")
   prtElt("<th>type</th>")
   prtElt("<th>ver</th>")
   prtElt("<th>link</th>")
   prtElt("</tr>")
   prtElt("</thead>")
   prtElt("<tbody>")
}

function prtEnd() {
   prtElt("</tbody>")
   prtElt("</table>")
}

.

$ ls
$
$ awk -f ../tst.awk ../file
$
$ ls
soft1  soft2

.

$ cat soft1
<table>
   <thead>
       <tr>
           <th>type</th>
           <th>ver</th>
           <th>link</th>
       </tr>
   </thead>
   <tbody>
       <tr>
           <td>soft1</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft1</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft1</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
       <tr>
           <td>soft1_beta_ver</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft1_beta_ver</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft1_beta_ver</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
       <tr>
           <td>soft1_alpha_ver</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft1_alpha_ver</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft1_alpha_ver</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
   </tbody>
</table>

.

$ cat soft2
<table>
   <thead>
       <tr>
           <th>type</th>
           <th>ver</th>
           <th>link</th>
       </tr>
   </thead>
   <tbody>
       <tr>
           <td>soft2</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft2</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft2</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
       <tr>
           <td>soft2_beta_ver</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft2_beta_ver</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft2_beta_ver</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
       <tr>
           <td>soft2_alpha_ver</td>
           <td>V1.0.1</td>
           <td>http://example.com/v1.0.1.zip</td>
       </tr>
       <tr>
           <td>soft2_alpha_ver</td>
           <td>V1.0.2</td>
           <td>http://example.com/v1.0.2.zip</td>
       </tr>
       <tr>
           <td>soft2_alpha_ver</td>
           <td>V1.0.3</td>
           <td>http://example.com/v1.0.3.zip</td>
       </tr>
   </tbody>
</table>

上面是針對這個輸入文件執行的:

$ cat ../file
- soft1:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft1_beta_ver:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft1_alpha_ver:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft2:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft2_beta_ver:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip
- soft2_alpha_ver:
   V1.0.1: http://example.com/v1.0.1.zip
   V1.0.2: http://example.com/v1.0.2.zip
   V1.0.3: http://example.com/v1.0.3.zip

引用自:https://unix.stackexchange.com/questions/544911