开发者

Editing html file with bash sed

I've generated a html table in bash, stored it in a variable and need to replace a specific region in a html with it.

sed -r -i~ -e "s/(var filesystem=)(.*)/var filesystem=\"$filesystem_table\";/g" ./Html/xmon/xmon.html

Basically i've turned the df -h bash output to a table and want to insert it with help of javascript into my page.

All my attempts return

sed: -e expression #1, char 40: unterminated `s' command

Is there no easier way to do this?

The value of $filesystem_table is: (corrected)

<tr> <td> Filesystem </td> <td> Size </td> <td> Used </td> <td> Avail </td> <td> Use% </td> <td> Mounted </td> </tr> <tr> <td> /dev/sda1 </td> <td> 94G </td> <td> 80G </td> <td> 9.3G </td> <td> 90% </td> <td> / </td> </tr> <tr> <td> none </td> <td> 2.0G </td> <td> 328K </td> <td> 2.0G </td> <td> 1% </td> <td> /dev </td> </tr> <tr> <td> none </td> <td> 2.0G </td> <td> 2.4M </td> <td> 2.0G </td> <td> 1% </td> <td> /dev/shm </td> </tr> <tr> <td> none </td> <td> 2.0G </td> <td> 372K </td> <td> 2.0G </td> <td> 1% </td> <td> /var/run </td> </tr> <tr> &开发者_开发知识库lt;td> none </td> <td> 2.0G </td> <td> 0 </td> <td> 2.0G </td> <td> 0% </td> <td> /var/lock </td> </tr> <tr> <td> /dev/sda3 </td> <td> 198G </td> <td> 184G </td> <td> 15G </td> <td> 93% </td> <td> /media/Zeta </td> </tr> <tr> <td> /dev/mmcblk0p1 </td> <td> 7.7G </td> <td> 477M </td> <td> 7.2G </td> <td> 7% </td> <td> /media/NIKON </td> </tr>

Solved! Partially thanks to Ignacio Vazquez-Abrams.

This is how I convert the outpout of 'df -h' to a html table:

df -h > /tmp/filesystem
            echo "<table>" > /tmp/filesystem_table
            cat /tmp/filesystem | while read line
            do
            echo "<tr>" >> /tmp/filesystem_table
            echo "<td>" >> /tmp/filesystem_table
            echo $line | awk '{ print $1; }' >> /tmp/filesystem_table
            echo "</td>" >> /tmp/filesystem_table
            echo "<td>" >> /tmp/filesystem_table
            echo $line | awk '{ print $2; }' >> /tmp/filesystem_table
            echo "</td>" >> /tmp/filesystem_table
            echo "<td>" >> /tmp/filesystem_table
            echo $line | awk '{ print $3; }' >> /tmp/filesystem_table
            echo "</td>" >> /tmp/filesystem_table
            echo "<td>" >> /tmp/filesystem_table
            echo $line | awk '{ print $4; }' >> /tmp/filesystem_table
            echo "</td>" >> /tmp/filesystem_table
            echo "<td>" >> /tmp/filesystem_table
            echo $line | awk '{ print $5; }' >> /tmp/filesystem_table
            echo "</td>" >> /tmp/filesystem_table
            echo "<td>" >> /tmp/filesystem_table
            echo $line | awk '{ print $6; }' >> /tmp/filesystem_table
            echo "</td>" >> /tmp/filesystem_table
            echo "</tr>" >> /tmp/filesystem_table
            done    
            echo "</table>" >> /tmp/filesystem_table

            filesystem_table="$($_CMD cat /tmp/filesystem_table)"
            filesystem_table="$($_CMD echo $filesystem_table)"  


            sed -r -i~ -e "s!(var filesystem=)(.*)!var filesystem=\"$filesystem_table\";!g" ./Html/xmon/xmon.html


You have a / in your env var. Use a different delimiter.

s!foo!bar!g


You can remove a lot of repetition by only specifying your output file once.

while ...
do
    ...
done >> /tmp/filesystem_table

You may not need the first temporary file at all.

{
echo "<table>"
df -h  | while read line
         do
             echo "<tr>"
             ...
         done
}  > /tmp/filesystem_table

Why use all those echo and awk commands when one call to AWK can do it all?

df -h | awk 'BEGIN { OFS = "\n</td>\n<td>\n";
                     print "<table>" }

             NR == 1 { ORS = "\n</td>\n</tr>\n";
                       $6 = $6 " " $7; NF = 6}    # fix "Mounted on"

             {
                printf "%s\n%s\n", "<tr>", "<td>"
                $1=$1    # recompute the line with new OFS and ORS
                print
             }

            END { ORS = "\n"
                  print "</table>" }' > /tmp/filesystem_table

You're doing some capture groups in your sed command, but not using them. It's unclear what your intention is.

By the way, sed and df are not part of Bash. They are separate utilities.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜