将分隔符保留在 cut 的输出中

use*_*624 5 bash solaris cut

我有一个脚本,用于cut从包含绝对路径的搜索中挑选一些信息。它看起来像:

PLACE=$(grep foo flatfile.txt | cut -d '/' -f 1-6)
Run Code Online (Sandbox Code Playgroud)

输出如下所示:

machine1:/path/to/where/foo/is
machine2:/another/path/to/find/foo
Run Code Online (Sandbox Code Playgroud)

我需要它看起来像这样:

machine1:/path/to/where/foo/is/
machine2:/another/path/to/find/foo/
Run Code Online (Sandbox Code Playgroud)

这需要在脚本末尾用echo "$PLACE"类似的东西打印到控制台。输出始终至少有 2 行,但通常更多。

我用 echo 尝试了我能想到的一切,但它要么根本不显示输出,要么给出输出:

grep: '/' is a directory
Run Code Online (Sandbox Code Playgroud)

我正在 Solaris 上运行 bash 3.00,如果这有帮助的话。我真的很想通过在 cut 命令的末尾添加一些东西来解决这个问题,而不必胡闹 sed 或 awk。但是,如果这是唯一的方法,那就这样吧。

mic*_*501 4

尝试这个 :

PLACE=$(grep foo flatfile.txt | cut -d '/' -f 1-6 | xargs -I "%" echo %/)
Run Code Online (Sandbox Code Playgroud)