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
    • 最新
    • 标签
主页 / computer / 问题 / 1855856
Accepted
John
John
Asked: 2024-09-16 02:09:32 +0800 CST2024-09-16 02:09:32 +0800 CST 2024-09-16 02:09:32 +0800 CST

if then 语句仅对匹配字符串的第一个实例起作用

  • 772

我在这里缺少一些知识,也没有合适的语言来描述(抱歉)。这是我的命令,它只在找到“$WARN”中的匹配项时返回第一个实例

echo "$WARN"

(W) 2024-08-30T08:08:52 - Restart is required to toggle support
(W) 2024-08-30T09:59:30 - Failed to download RSS feed at 'https://aaa'. Reason: The connection to the remote server timed out
(W) 2024-08-30T10:00:30 - Failed to download RSS feed at 'https://bbb'. Reason: The connection to the remote server timed out
(W) 2024-08-30T10:00:30 - Failed to download RSS feed at 'https://ccc'. Reason: The connection to the remote server timed out
(W) 2024-09-13T08:59:44 - Failed to download RSS feed at 'http://xxx'. Reason: The remote server refused the connection
(W) 2024-09-13T08:59:44 - Failed to download RSS feed at 'http://yyy'. Reason: The remote server refused the connection
(W) 2024-09-13T08:59:44 - Failed to download RSS feed at 'http://zzz'. Reason: The remote server refused the connection

WARN=$(
  if [[ "$WARN" == *Failed* ]]
  then
  printf '%s' "${WARN//''\'*''\'\./...}"
  fi
)

echo "$WARN"

(W) 2024-08-30T08:08:52 - Restart is required to toggle support
(W) 2024-08-30T09:59:30 - Failed to download RSS feed at ... Reason: The connection to the remote server timed out

预期输出:唯一行和与“失败”匹配的行被修改以删除单引号内的所有内容,包括引号本身。

(W) 2024-08-30T08:08:52 - Restart is required to toggle support
(W) 2024-08-30T09:59:30 - Failed to download RSS feed at ... Reason: The connection to the remote server timed out
(W) 2024-09-13T08:59:44 - Failed to download RSS feed at ... Reason: The remote server refused the connection

实际输出:为什么省略了第二行的考虑,即只有包含的第一个匹配的行Failed被执行并返回。

echo "$WARN"

(W) 2024-08-30T08:08:52 - Restart is required to toggle support
(W) 2024-08-30T09:59:30 - Failed to download RSS feed at ... Reason: The connection to the remote server timed out

如果我对输入进行排序,则将其反转,第二次匹配的出现Failed会被省略。

echo "$WARN"

(W) 2024-09-13T08:59:44 - Failed to download RSS feed at ... Reason: The remote server refused the connection
(W) 2024-08-30T08:08:52 - Restart is required to toggle support

我不想匹配字符串的末尾。

我第二次创建变量 $WARN 时,用新内容替换它。

bash
  • 1 1 个回答
  • 21 Views

1 个回答

  • Voted
  1. Best Answer
    Kamil Maciorowski
    2024-09-16T03:14:13+08:002024-09-16T03:14:13+08:00

    最初,WARN变量包含多行。不过,与任何非数组 shell 变量一样,它只是一个字符串,而且里面有换行符这一事实不会改变这一点。

    [[ "$WARN" == *Failed* ]]Failed 测试字符串中是否存在子字符串。这是针对整个字符串进行的一次测试,而不是“每行”测试。

    类似地,中的替换${WARN//\'*\'./...}(为简洁起见,我删除了不必要的反斜杠和两对包含空字符串的单引号)一次作用于整个字符串,而不是“每行”;并且由于是*贪婪的,因此存在一个匹配,该匹配从第一个“失败”行开始,到最后一个“失败”行结束。换句话说,匹配包括几个单引号和几个换行符,所有这些都被替换为...。

    换句话说:...您在输出中看到的行由某行的开头、和不同...行的结尾组成。

    使用一次处理一行的工具,例如sed:

    <<<"$WARN" sed "/Failed/ s/'.*'\\./.../"
    

    注意.*是'.*'贪婪的,它可以匹配包含'(甚至是换行符)的字符串;但是现在没有问题,因为我们sed分别处理每一行。

    您可以通过管道过滤掉“类似”的行uniq:

    <<<"$WARN" sed "/Failed/ s/'.*'\\./.../" | uniq -f 3
    

    uniq -f 3忽略前三个字段,即(W)、时间戳和-(就您而言)。这样,不同的时间戳不会妨碍行被视为相等,只有后面的内容才重要。这似乎就是您想要的。

    可以实现更复杂的逻辑,例如使用awk。

    也许你可以不使用变量来做到这一点。如果变量的值来自输出,那么some_tool这可能就足够了:

    some_tool | sed "/Failed/ s/'.*'\\./.../" | uniq -f 3
    

    如果变量的值来自a_file那么这可能就足够了:

    <a_file sed "/Failed/ s/'.*'\\./.../" | uniq -f 3
    
    • 1

相关问题

  • 在非 root 用户上用 bash 替换 zsh

  • 在 macOS High Sierra 的终端中设置环境变量时遇到问题

  • 对于 cp 或 mv,是否有等同于 cd - 的东西?

  • Notify-发送窗口下出现的通知

  • 如何从 WSL 打开 office 文件

Sidebar

Stats

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

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve