Bash突破线与sed?

时间:2014-01-20 16:06:13

标签: bash sed

当我在下面运行这个bash脚本时,我的结果都在同一行上回显,如下面的“示例输出”所示。

#!/bin/bash
Commander=$(nmap -vv -p 8080 xxx.149.xxx.100-xxx | 
            grep "Discovered open port 8080/tcp     on" | sed -r 's/^.{32}//');
echo $Commander;

示例输出:

xx.149.xx.115 xx.149.xx.107 xx.149.xx.107 xx.149.xx.101 xx.149.xx.118 xx.149.xx.146

我想要的是:

xx.149.xx.115 
xx.149.xx.107 
xx.149.xx.107 
xx.149.xx.101 
xx.149.xx.118 
xx.149.xx.146 
xx.149.xx.19

任何,建议??

1 个答案:

答案 0 :(得分:4)

在调用时只需引用变量:

echo "$Commander"

示例

$ myvar="hello
> i am fedor
> fedorqui"

$ echo $myvar
hello i am fedor fedorqui

$ echo "$myvar"
hello
i am fedor
fedorqui

所以你有空格分隔输出,你希望它是新的行分隔?如果是这样,请通过管道tr ' ' '\n'进行替换。

$ d="a b c"
$ echo "$d" | tr ' ' '\n'
a
b
c