【问题标题】:grep and cut command to get output in different lines [duplicate]grep 和 cut 命令在不同的行中获取输出[重复]
【发布时间】:2022-12-12 12:21:10
【问题描述】:
$ cat hello.txt     
shreenivasa hi hello this is test mail                      
shreenivasa how are you man

如果我在终端运行

$ cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-

它给出以下输出

hi hello this is test mail      
how are you man

但是如果我把它写成hello.sh中的脚本,如下所示

#!/bin/bash
hi=`cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-`
echo $hi

./hello.sh 的输出是

hi hello this is test mail how are you man.

我想在下一行打印“你好吗” 喜欢

hi hello this is test mail     
how are you man

试过 $hi\n 没用

【问题讨论】:

标签: bash


【解决方案1】:

你需要引用你的变量。

$ echo "$hi"

此外,您应该将命令放在函数而不是变量中

$ hi () {grep 'shreenivasa' hello.txt | cut -d ' ' -f 2-; }

输出

$ hi
hi hello this is test mail
how are you man

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    相关资源
    最近更新 更多