Bash 如何将十进制/浮点格式输出写入文件?

Bash 如何将十进制/浮点格式输出写入文件?,bash,shell,Bash,Shell,我已经以十六进制格式输出了转换矩阵,但我希望转换矩阵是十进制/浮点格式。我使用下面的bash脚本将矩阵文件转换为十进制格式 你能告诉我如何将输出写入文件吗 文件的输入包含十六进制格式,并返回存储十进制格式的新文件 这是我的代码: #!/bin/bash # Read from specified file, or from standard input infile="${1:-/dev/stdin}" outfile="${2:-/dev/stdout}" while read line;

我已经以十六进制格式输出了转换矩阵,但我希望转换矩阵是十进制/浮点格式。我使用下面的bash脚本将矩阵文件转换为十进制格式

你能告诉我如何将输出写入文件吗

文件的输入包含十六进制格式,并返回存储十进制格式的新文件

这是我的代码:

#!/bin/bash

# Read from specified file, or from standard input
infile="${1:-/dev/stdin}"
outfile="${2:-/dev/stdout}"

while read line; do

    for number in $line; do
        a_dec="%f" "$number"
        echo $a_dec >> $outfile
    done
    echo
done < $infile



假设
number
始终是一个数字(因此您不需要验证它),您只需修改
a_dec=
分配即可使用
printf
。此外,由于
printf
可以接受多个参数,因此可以省略内部循环:

#!/bin/bash

# Read from specified file, or from standard input
infile="${1:-/dev/stdin}"
outfile="${2:-/dev/stdout}"

while read line; do

    printf "%f " $line
    echo

    # echo $(printf "%f\n" $line)    # or this to elide the trailing space

done <"$infile" >"$outfile"
#/bin/bash
#从指定文件或标准输入读取
infle=“${1:-/dev/stdin}”
outfile=“${2:-/dev/stdout}”
读行时;做
printf“%f”$行
回声
#echo$(printf“%f\n”$行)#或此选项以删除尾随空格
完成“$outfile”

/dev/stdout
?对不起,我不知道。这是一场灾难的结果software@DiegoTorresMilano谢谢但是仍然有错误
:第10行:未找到a_dec:command
(提示:
=
)谢谢。我已经改正了所有的错误。我运行它,得到错误
第10行:fg:no job control
输出中没有任何内容。您能检查一下吗?另外,我想在运行scrip againIt时写入新文件而不是追加,但必须在一行中的数字之间添加空格。您关心行的末尾是否有空格吗?不,仅在两个数字之间。
#!/bin/bash

# Read from specified file, or from standard input
infile="${1:-/dev/stdin}"
outfile="${2:-/dev/stdout}"

while read line; do

    printf "%f " $line
    echo

    # echo $(printf "%f\n" $line)    # or this to elide the trailing space

done <"$infile" >"$outfile"