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 / 问题 / 1461500
Accepted
Backy
Backy
Asked: 2023-03-30 04:00:10 +0800 CST2023-03-30 04:00:10 +0800 CST 2023-03-30 04:00:10 +0800 CST

将多个文件模式传递给 grep

  • 772

我有一系列搜索模式存储在 bash 数组 ( ptrn) 中,我想将其传递给grep命令。我该怎么做?

  ptrn=("FN" "DS")
  for fl in "$@"; do  # loop through number of files
    if [[ -f "$fl" ]]; then
      printf '\n%s%s%s\n\n' "$mgn" "==>  $flnm  <==" "$rst"
      grep --color "$ptrn" "$flnm"
    fi
  done
bash
  • 2 2 个回答
  • 39 Views

2 个回答

  • Voted
  1. Best Answer
    Thor
    2023-03-30T06:07:04+08:002023-03-30T06:07:04+08:00

    如何grep通过子 shell 提供模式,例如:

    grep -f <(printf "%s\n" ${ptrn[@]}) FILE...
    
    • 2
  2. Raffa
    2023-03-30T05:05:00+08:002023-03-30T05:05:00+08:00

    如果保证存储为数组元素的模式/单词中没有空格或非转义 shell 特殊字符,则可以使用 的bash参数扩展将数组元素作为grep单独的单独模式传递给-e FN -e DS ...,例如:

    ptrn=("FN" "DS")
    
    # Prepend "-e" to each array element
    ptrn2="${ptrn[@]/#/-e }"
    
    # Don't quote "$ptrn2" or it will be passed as a single token and wouldn't work.
    grep --color $ptrn2 file
    

    |或者,如果它们可能包含非转义的 shell 特殊字符,您可以围绕(或)构建正则表达式(也拆分所有空格但不会失败)并将其与类似的东西一起使用:

    ptrn=("FN" "DS")
    
    # Translate spaces " " single or multiple including spaces between words in a single array element into "|".
    ptrn2="$(echo ${ptrn[@]} | tr " " "|")"
    
    # -E enables extended regular expressions ... needed for this to work.
    grep --color -E "$ptrn2" file
    

    |或者要在每个正则表达式模式中保留精确空间,将每个数组元素作为单独的标记传递,并使用(逻辑或)构建它们的扩展正则表达式,您可以执行以下操作:

    ptrn=("FN" "DS")
    
    # Append "|" to each array element.
    ptrn2="$(printf '%s|' "${ptrn[@]}")"
    
    # Delete the last character i.e. the extra "|".
    ptrn2="${ptrn2::-1}"
    
    # -E enables extended regular expressions ... needed for this to work.
    grep --color -E "$ptrn2" file
    
    • 0

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

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