You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bash中删除包含‘Date:’的行之后的所有内容?

过滤git show输出,保留至Date:行

可以直接通过管道结合文本处理工具,一步完成输出过滤,无需额外文件操作:

方法1:使用awk

执行以下命令即可直接得到目标输出:

git show --name-only | awk '/^Date:/{print; exit}1'

原理说明

  • awk逐行读取输入内容
  • 当匹配到以Date:开头的行时,打印该行并立即退出程序
  • 1是awk的简写,表示打印当前行(对所有未触发退出条件的行生效)

方法2:使用sed

也可以用sed实现同样效果:

git show --name-only | sed '/^Date:/q'

原理说明

  • sed的q命令表示匹配到指定模式后立即退出
  • 该命令会打印包括Date:行在内的所有前置内容,然后终止处理

效果示例

原git show --name-only输出:

commit <commit-hash> (HEAD -> <branch-name>, origin/<branch-name>)
Author: bitbucket-pipelines <commits-noreply@bitbucket.org>
Date:   Mon Sep 18 18:26:04 2023 +0000

    [skip ci] Commit message

logs/log.txt

执行上述命令后,输出结果:

commit <commit-hash> (HEAD -> <branch-name>, origin/<branch-name>)
Author: bitbucket-pipelines <commits-noreply@bitbucket.org>
Date:   Mon Sep 18 18:26:04 2023 +0000

内容的提问来源于stack exchange,提问作者kuzmych

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 15:47:27