AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1036912
Accepted
Josef Klimuk
Josef Klimuk
Asked: 2018-05-17 03:18:53 +0800 CST2018-05-17 03:18:53 +0800 CST 2018-05-17 03:18:53 +0800 CST

使用 sed 命令时如何获取退出状态?

  • 772

该grep命令给出退出状态:

$echo "foo.bar" | grep -o foo
foo
$echo $?
0
$echo "foo.bar" | grep -o pop
$echo $?
1  

但我需要使用sed,我意识到它没有退出状态:

$echo "foo.bar" | sed 's/bar.*$//'
foo.
$echo $?
0
$echo "foo.bar" | sed 's/pop.*$//'
foo.bar
$echo $?
0  

我知道我应该玩这个-q选项,但我没有成功。

command-line sed
  • 3 3 个回答
  • 10717 Views

3 个回答

  • Voted
  1. Best Answer
    steeldriver
    2018-05-17T03:29:16+08:002018-05-17T03:29:16+08:00

    您可以使用 q n以退出状态n退出- 但要使其有用,您还需要使用一些Branching 和 Flow Control:

    t仅当 在读取最后一个输入行后命令成功或采用另一个条件分支时,
    才有条件分支(即:跳转到标签) 。s///

    最好为n选择一个不同于标准退出状态值之一的值:

    退出状态为零表示成功,非零值表示失败。GNU 'sed' 返回以下退出状态错误值:

    0
     Successful completion.
    
    1
     Invalid command, invalid syntax, invalid regular expression or a
     GNU 'sed' extension command used with '--posix'.
    
    2
     One or more of the input file specified on the command line could
     not be opened (e.g.  if a file is not found, or read permission is
     denied).  Processing continued with other files.
    
    4
     An I/O error, or a serious processing error during runtime, GNU
     'sed' aborted immediately.
    

    所以例如

    $ echo "foo.bar" | sed 's/bar.*$//; t; q42' ; echo $? 
    foo.
    0
    

    然而

    $ echo "foo.bar" | sed 's/baz.*$//; t; q42' ; echo $? 
    foo.bar
    42
    

    如果您想省略模式空间的默认打印,请替换q为Q(注意这Q是 GNU 扩展)。

    • 14
  2. Noam Manos
    2020-01-06T07:42:50+08:002020-01-06T07:42:50+08:00

    以下是如何使用 sed 搜索正则表达式,并突出显示匹配项,如果未找到匹配项,则返回退出代码(5):

    这是 input.txt:

    hello there
    my dear old friend
    a lot of things
    we'll pass along the end
    

    这是我打印所有 + 突出显示匹配 + 返回退出代码的功能:

    highlight()
    {
      pattern=$1
      shift
      sed '/'"${pattern}"'/,${s//\x1b[32m&\x1b[0m/g;b};$q5' "$@"
    }
    

    $ highlight "lo\|end" input.txt || echo -e "\n* 未找到匹配项 *"

    你好, 亲爱 的老朋友_ _ _ _ _ _ _


    当没有匹配时,它将返回退出代码 (5)。您可以将它与 cat 和 pipe | 一起使用 还有:

    $ 猫输入.txt | 突出显示“热\|和” || echo -e "\n* 未找到匹配项 *"

    你好,
    我亲爱的老朋友
    很多事情
    我们会在最后传递

    * 未找到匹配项 *

    感谢https://unix.stackexchange.com/a/405725/43233 - 我正在使用它 + sed 退出选项。

    • 1
  3. milahu
    2021-10-31T12:11:31+08:002021-10-31T12:11:31+08:00

    重复答案https://stackoverflow.com/a/61808364/10440128

    #!/bin/sh
    
    read -d '' input <<EOF
    a
    b
    EOF
    
    echo "replace yes:"
    echo "$input" >input.txt
    sed -i 's/a/x/ w /dev/stdout' input.txt
    
    echo "replace no:"
    echo "$input" >input.txt
    sed -i 's/z/x/ w /dev/stdout' input.txt
    

    输出:

    replace yes:
    x
    replace no:
    

    所以,要检查 sed 是否替换了某些东西:

    #!/bin/sh
    
    read -d '' input <<EOF
    a
    b
    EOF
    
    echo "$input" >input.txt
    if [ -z "$(sed -i 's/a/x/w /dev/stdout' input.txt)" ]
    then echo "replace no" # -z = "zero string"
    else echo "replace yes"
    fi
    
    echo "$input" >input.txt
    if [ -n "$(sed -i 's/z/x/w /dev/stdout' input.txt)" ]
    then echo "replace yes" # -n = "nonzero string"
    else echo "replace no"
    fi
    

    输出:

    replace yes
    replace no
    

    限制:只能处理一个替换。所以这不起作用:

    sed -i 's,a,b, w /dev/stdout ; s,b,a, w /dev/stdout' input.txt
    

    错误:

    sed: couldn't open file /dev/stdout ; s,b,a, w /dev/stdout: No such file or directory
    

    (sed 将之后的所有内容w视为文件路径)

    解决方法:使用多个 sed 调用

    #!/bin/sh
    
    read -d '' input <<EOF
    a
    b
    EOF
    
    echo "$input" >input.txt
    if [ -n "$(
      sed -i 's/a/x/w /dev/stdout' input.txt &&
      sed -i 's/x/a/w /dev/stdout' input.txt
    )" ]
    then echo "replace yes"
    else echo "replace no"
    fi
    
    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve