从 grep coutput 获取关键字

发布于 2025-01-15 20:14:52 字数 934 浏览 3 评论 0原文

我有一个现有的基本脚本,无论我 grep cat 还是狗,我都需要从中获取 SERIAL 的值。像这样:

# grep dog | <additional commands>
123456789

script = Animals.sh

#!/bin/bash

ANIMAL=$1

case "${ANIMAL}" in
        dog)
                echo ""
                echo "${ANIMAL} animal is chosen"
                SERIAL=123456789
                ;;
        cat)
                echo ""
                echo "${ANIMAL} animal is chosen"
                SERIAL=987654321
                ;;
        *)
                echo ""
                echo "wrong parameter applied"
                exit 1
                ;;
esac

到目前为止,我已经尝试过这些,但是我仍在尝试研究如何继续或修剪其他输出

[ec2-user@ip-192-168-100-101 ~]$ grep dog -A 3 animals.sh 
        dog)
                echo ""
                echo "${ENV} animal is chosen"
                SERIAL=123456789
[ec2-user@ip-192-168-100-101 ~]$ 

I have an existing basic script that I need to get the value of SERIAL only from it whether I grep cat or dog. Like this:

# grep dog | <additional commands>
123456789

The script = animals.sh

#!/bin/bash

ANIMAL=$1

case "${ANIMAL}" in
        dog)
                echo ""
                echo "${ANIMAL} animal is chosen"
                SERIAL=123456789
                ;;
        cat)
                echo ""
                echo "${ANIMAL} animal is chosen"
                SERIAL=987654321
                ;;
        *)
                echo ""
                echo "wrong parameter applied"
                exit 1
                ;;
esac

So far I have tried these however I'm still trying to research on how to continue or trim the other outputs

[ec2-user@ip-192-168-100-101 ~]$ grep dog -A 3 animals.sh 
        dog)
                echo ""
                echo "${ENV} animal is chosen"
                SERIAL=123456789
[ec2-user@ip-192-168-100-101 ~]$ 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

━╋う一瞬間旳綻放 2025-01-22 20:14:53

如果 awk 是您的选择,请尝试:

awk '
/dog/ {f = 1}                   # if the line matches "dog", set a flag
f && /SERIAL/ {                 # if the flag is set and the line matches "SERIAL"
    sub(".*SERIAL=", "")        # remove substring before the number
    print                       # print the result
    exit
}
' animals.sh

输出:

123456789

如果您更喜欢 grep 解决方案,这里有一个替代方案:

grep -Poz 'dog[\s\S]+?SERIAL=\K\d+' animals.sh

说明:

  • -P选项启用 PCRE 正则表达式。
  • -o 选项告诉 grep 仅打印匹配的子字符串
  • -z 选项将记录分隔符设置为空字符,从而使
    跨行的模式匹配。

关于正则表达式:

  • dog[\s\S]+?SERIAL= 匹配最短的字符串(包括换行符)
    介于 dogSERIAL= 之间(含)。
  • \K 告诉正则表达式引擎丢弃前面的匹配项
    匹配的模式。它的作用是清除\K之前的匹配模式。
  • \d+ 匹配将作为结果打印的数字序列。

If awk is your option, would you please try:

awk '
/dog/ {f = 1}                   # if the line matches "dog", set a flag
f && /SERIAL/ {                 # if the flag is set and the line matches "SERIAL"
    sub(".*SERIAL=", "")        # remove substring before the number
    print                       # print the result
    exit
}
' animals.sh

Output:

123456789

If you prefer grep solution, here is an alternative:

grep -Poz 'dog[\s\S]+?SERIAL=\K\d+' animals.sh

Explanation:

  • The -P option enables PCRE regex.
  • The -o option tells grep to print only the matched substring
  • The -z option sets the record separator to the null character, enabling
    the pattern match across lines.

About the regex:

  • dog[\s\S]+?SERIAL= matches a shortest string (including newline characters)
    between dog and SERIAL= inclusive.
  • \K tells the regex engine to discard preceding matches out of the
    matched pattern. It works to clear the matched pattern before \K.
  • \d+ matches a sequence of digits which will be printed as a result.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文