0

我想知道是否有人可以帮助我解决这个小问题。我想通过网页查看文本文件。但是,文本文件没有换行符或格式。

有没有办法在查看时临时添加换行符?

我想通过 shell 脚本来实现这一点。

示例文本文件:

    This is a sample text file.

    Same concept.

    Text has no format. 100 of lines...

也许不是最好的解决方案......但我使用以下方法来完成这项工作:

解决方案:

    FILE=$rpt

    while read LINE; do
        echo "$LINE<br>"
    done < "$FILE"
4

2 回答 2

1

你可以试试这个脚本:

#!/bin/sh

FILE=$1
TEMP=$(mktemp /tmp/XXXXXXXX.html)
BROWSER=firefox

{
    echo "<html><head></head><body><div>"
    while read LINE; do
        echo "$LINE<br>"
    done
    echo "</div></body></html>"
} < "$FILE" > "$TEMP"

"$BROWSER" "$TEMP"
# sleep 5  ## If your browser launches in the background. Firefox doesn't.
rm -f "$TEMP"

运行它作为sh script.sh /path/to/file.txt.

于 2013-09-05T02:23:02.990 回答
0

当您的网络服务器发送文件时,它们会以与文件类型相对应的“内容类型”发送。您的网络服务器听起来可能是作为 type 发送的text/html,因此浏览器会期望该文件是 HTML 格式的。

看看您是否可以指示您的网络服务器将您的文件发送为text/plain,这可能就像将文件重命名为具有.txt扩展名一样简单。

于 2013-09-05T01:42:37.833 回答