使用 bash 将逗号分隔的列表导出到 html

Ary*_*reh 2 html bash shell css

我有一个 cxomma 分隔的 csv,我想将其转换为自定义 html,并且我遇到了另一个执行此操作的脚本,但是自从我使用https://www.tutorialspoint.com/execute_bash_online以来,这对我来说只是问题.php来运行它。

这是我找到的脚本:https : //unix.stackexchange.com/questions/105501/convert-csv-to-html-table

我的问题是我的输出需要有点不同,而关于 ECHO 的一些东西真的是在抗争。所以这是我的清单的一个例子:

Product,100 usd 现在使用这个脚本我想把它转换成这样:Product 100 usd

基本上将列表分成 2 个单元格。但是 echo 不断剥离 "'s 所以它只是一团糟。我不知道是不是因为 bash 模拟器换行,但我不断收到错误,似乎假设 >< 和 tr 以及 td 是一个命令.

目前我的脚本看起来像这样,它反映了我对 echo.. 的困扰:

while read INPUT ; do
echo ""<tr onmouseover="this.style.backgroundColor='#ffff66';"";
echo ""onmouseout="this.style.backgroundColor='#d4e3e5';>";
echo ""<td>${INPUT//,/</td><td><b>}</b></td></tr>";""
done
Run Code Online (Sandbox Code Playgroud)

这会产生:

$bash -f main.sh
main.sh: line 2: tr: No such file or directory
main.sh: line 4: ;
echo <td>six</td><td><b>nine</td><td><b>twelve</b></td></tr>: No such file or directory
main.sh: line 4: : command not found 
Run Code Online (Sandbox Code Playgroud)

我已经疯狂地尝试了回声,但现在我已经无能为力了。任何和所有帮助都非常感激地接受。

gle*_*man 7

您的引用是错误的,特别是双双引号。

让我们分解你所拥有的,这些位连接在一起

echo ""<tr onmouseover="this.style.backgroundColor='#ffff66';"";
Run Code Online (Sandbox Code Playgroud)
  1. 回声命令
  2. 空字符串 ""
  3. 重定向<tr——这是“找不到文件”错误的来源
  4. 空间
  5. 一个字符串 onmouseover="this.style.backgroundColor='#ffff66';"
  6. 一个开引号和分号,下一个字符串的第一个字符

要修复您的 echo 命令:

while read INPUT ; do
echo "<tr onmouseover=\"this.style.backgroundColor='#ffff66'\""
echo "onmouseout="this.style.backgroundColor='#d4e3e5'>"
echo "<td>${INPUT//,/<\/td><td><b>}</b></td></tr>"
done
Run Code Online (Sandbox Code Playgroud)

当您混合使用单引号和双引号时,heredoc 非常易读。此外,使用该read命令分隔 CSV 行。

while IFS=, read -r first second ; do
    cat <<END_HTML
        <tr onmouseover="this.style.backgroundColor='#ffff66'" onmouseout="this.style.backgroundColor='#d4e3e5'">
        <td>$first</td><td><b>$second</b></td>
        </tr>
END_HTML
done
Run Code Online (Sandbox Code Playgroud)

  • `echo` 不从 stdin 读取,因此此处的 heredoc 没有用处。也许您打算在最后一个片段中使用“cat”? (2认同)