bash 在bash脚本中查找包含所有关键字的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1829239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:26:54  来源:igfitidea点击:

Find lines containing all keywords in bash script

bashgrep

提问by Siou

Essentially, I would like something that behaves similarly to:

本质上,我想要一些行为类似于:

cat file | grep -i keyword1 | grep -i keyword2 | grep -i keyword3

How can I do this with a bash script that takes a variable-length list of keyword arguments? The script should do a case-insensitive match of lines containing all keywords.

如何使用带有可变长度关键字参数列表的 bash 脚本执行此操作?该脚本应该对包含所有关键字的行进行不区分大小写的匹配。

回答by Idelic

Use this as a script

将此用作脚本

#! /bin/bash
awk -v IGNORECASE=1 -f <(
  P=; for k; do [ -z "$P" ] && P="/$k/" || P="$P&&/$k/"; done
  echo "$P{print}"
)

and invoke it as

并将其调用为

script.sh keyword1 keyword2 keyword3 < file

回答by TheBonsai

I don't know if this is efficient, and I think this is ugly, also there might be some utility for that, but:

我不知道这是否有效,我认为这很丑陋,也可能有一些效用,但是:

#!/bin/bash

unset keywords matchlist
keywords=("$@")

for kw in "${keywords[@]}"; do
matchlist="$matchlist /$kw/ &&"
done

matchlist="${matchlist% &&}"

# awk "$matchlist { print; }" < <(tr '[:upper:]' '[:lower:]' <file)
awk "$matchlist { print; }" file

And yes, it needs some robustness regarding special characters and stuff. It's just to show the idea.

是的,它需要一些关于特殊字符和内容的稳健性。这只是为了展示这个想法。

回答by Paused until further notice.

Give this a try:

试试这个:

shopt -s nocasematch
keywords="keyword1|keyword2|keyword3"
while read line; do [[ $line =~ $keywords ]] && echo $line; done < file

Edit:

编辑

Here's a version that tests for all keywords being present, not just any:

这是一个测试所有存在关键字的版本,而不仅仅是任何关键字:

keywords=(keyword1 keyword2 keyword3)    # or keywords=("$@")
qty=${#keywords[@]}
while read line
do
    count=0
    for keyword in "${keywords[@]}"
    do
        [[ "$line" =~ $keyword ]] && (( count++ ))
    done
    if (( count == qty ))
    then
        echo $line
    fi
 done < textlines

回答by ghostdog74

you can use bash 4.0++

你可以使用 bash 4.0++

shopt -s nocasematch
while read -r line
do
    case "$line" in 
        *keyword1*) f=1;;&
        *keyword2*) g=1;;&
        *keyword3*) 
            [ "$f" -eq 1 ] && [ "$g" -eq 1 ] && echo $line;;
    esac
done < "file"
shopt -u nocasematch

or gawk

或呆呆

gawk '/keyword/&&/keyword2/&&/keyword3/' file

回答by Siou

Found a way to do this with grep.

找到了一种使用 grep 执行此操作的方法。

KEYWORDS=$@
MATCH_EXPR="cat file"
for keyword in ${KEYWORDS};
do
  MATCH_EXPR="${MATCH_EXPR} | grep -i ${keyword}"
done
eval ${MATCH_EXPR}

回答by Andy Lester

I'd do it in Perl.

我会用 Perl 来做。

For finding all lines that contain at least one of them:

查找至少包含其中之一的所有行:

perl -ne'print if /(keyword1|keyword2|keyword3)/i' file

For finding all lines that contain all of them:

查找包含所有这些行的所有行:

perl -ne'print if /keyword1/i && /keyword2/i && /keyword3/i' file

回答by John

Here is a script called search.shin bash that will search lines within a file or folder for all keywords specified:

这是一个search.sh在 bash 中调用的脚本,它将在文件或文件夹中搜索所有指定关键字的行:

#!/bin/bash
if [ $# -lt 2 ]; then
    echo "[-] 
./search.sh tools.txt python jira
file_to_search/folder_to_search keyword1 keyword2 keyword3 ..." exit fi all_args="$@" i=0 results="" # this will store the cumulative results from each keyword search for arg in $all_args; do if [ $i -eq 0 ]; then # first argument is the file/folder to search file_to_search="$arg" i=$(($i + 1)) elif [ $i -eq 1 ]; then # search the file/folder with first keyword (first search) results=`grep --color=always -r -n -i "$arg" "$file_to_search"` i=$(($i + 1)) else # now keep searching the results from first search for other keywords results=`echo "$results" | grep --color=always -i "$arg"` i=$(($i + 1)) fi done echo "$results"

Example invocation of script above will search the 'tools.txt' file for 'python' and 'jira' keywords:

上面脚本的示例调用将在 'tools.txt' 文件中搜索 'python' 和 'jira' 关键字:

##代码##