【问题标题】:Grep -rl from a .txt list.txt 列表中的 Grep -rl
【发布时间】:2020-12-09 01:42:02
【问题描述】:

我正在尝试从 .txt 文件中查找字符串列表,搜索目标是多个 .csv 的目录(查找包含字符串的 .csv)

我已经找到了手动操作的方法: grep -rl doggo C:\dirofcsv\ 下一步是从数百个术语的列表中找到它。

我尝试了grep -rl -f list.txt C:\dirofcsv < print.txt,但我只打印了最后一个术语。我希望逐行显示结果。

我错过了一些东西,但我不知道在哪里。 我正在使用术语模拟器在 Windows 上工作。

编辑:我找到了如何列出文件中的术语。现在我需要查看哪些术语有哪些结果,例如“doggo => file2,file4”我需要编写一个循环吗?

感谢社区。​​p>

【问题讨论】:

    标签: bash loops unix search grep


    【解决方案1】:
    grep -rl -f list.txt C:\dirofcsv >> print.txt
    

    您希望在 print.txt 文件中添加行,因此需要使用 >> 而不是 >,这将覆盖文件中已有的内容。

    要获得您编辑的需求中所需的输出中列出的输出,您可以使用重定向回 awk 的循环:

    awk '/^FILE -/ { fil=$3; # When the output start with "FILE -" set fil to the third space delimited field
                     next # Skip to the next line
                   } 
                   { arr[fil][$0]="" # Set up a 2 dimensional array with the search term (fil) as the first index and the name of the file the second
                   } 
               END { for (i in arr) { # Loop through the array
                                      printf "%s => ",i; First print the search term in the format required
                                      for (j in arr[i]) { 
                                                          printf "%s,",j # Print the file name followed by a comma
                                                        } 
                                      printf "\n" # Print a new line
                                     } 
                    }' <<< "$(while read line # Read list.txt line by line
                              do 
                                 echo "FILE - $line"; Echo a marker for identification in awk
                                 grep -l "$line" C:\dirofcsv  ; # Grep for the line
                              done < list.txt)" >> print.txt
    

    一个班轮:

    awk '/^FILE -/ { fil=$3;next } { arr[fil][$0]="" } END { for (i in arr) { printf "%s => ",i;for (j in arr[i]) { printf "%s,",j } printf "\n" } }' <<< "$(while read line;do echo "FILE - $line";grep -l "$line"  C:\dirofcsv done < list.txt)" >> print.txt
    

    【讨论】:

    • 谢谢你,这就是我想要的!可以逐行打印吗?比如:doggo = file2,file5 mouse = file4,file2。因为我有所有的指纹,但不知道属于哪个术语。
    • 只需从 grep 中删除 l 标志
    • 它给了我太多信息,我需要将请求第 1 行与文件结果一起保留,然后在请求第 2 行中保留文件结果等。
    • 为此,您需要阅读 list.txt 的每一行,然后对该行执行 grep。
    • 我为循环添加了一个额外的解决方案
    【解决方案2】:

    我认为您的意思是将命令传递为:

    grep -rl -f list.txt C:\dirofcsv >> print.txt
    

    试一试。它应该逐行获取list.txt 中的所有模式,并在目录C:\dirofcsv 中搜索具有匹配模式的文件并将其名称打印到print.txt 文件中。

    试试这个不循环打印(就像你在 cmets 中问的那样 ;-)

    单行答案

    dir=C:\dirofcsv
    listfile=list.txt
    
    eval $(jq -Rsr 'split("\n") | map(select(length > 0)) | reduce .[] as $line ([]; . + ["echo \($line) :; grep -rl \($line) \($dir); echo"]) | (join("; "))' --arg dir "$dir" < "$listfile")
    

    另一种解决方案,为了解释说:

    unset li
    readarray li -u <"$listfile"
    quoted_commands="$(jq -R 'reduce inputs as $line ([]; . + ["echo \($line) :; grep -rl \($line) \($dir); echo"]) | (join("; "))' \
        --arg dir $dir \
        <<< $(echo; printf "%s" "${li[@]}"))"
    quoted_commands=${quoted_commands%\"}
    commands=${quoted_commands#\"}
    eval $commands
    

    分解命令以便在 cmets 中更好地解释:

    # read contents of listfile in li
    unset li && readarray li -u <"$listfile"
    
    # add the content to new list so that it prints the list elements in new-lines
    # also add a newline at top as it will be discarded by jq (in this case only)
    list="$(echo; printf "%s" "${li[@]}";)"
    
    # pass jq command
    quoted_commands="$(jq -R 'reduce inputs as $line
            ([]; . + ["echo \($line) :; grep -rl \($line) \($dir); echo"])
            | (join("; "))' \
        --arg dir $dir <<< "$list")"
    # the elements are read with reduce filter and converted to JSON Array of corresponding commands to execute
    # the commands for all elements of list are joined with join filter
    
    # trim quotes to execute commands properly
    commands=$(sed -e 's/^"//' -e 's/"$//' <<< "$quoted_commands")
    
    # run commands
    eval "$commands"
    

    您可能想要打印上述变量。这样做时请注意在 echo/printf 中使用引号,即echo "$variable"。

    替换sed命令:

    signgle_quoted=${quoted%\"}
    commands=${signgle_quoted#\"}
    echo "$commands"
    

    我现在使用以下实现(虽然字典实现使用for 循环,但key : value 实现没有,并且是单行命令):

    # print an Associative bash array as a JSON dictionary
    print_dict()
    {
        declare -n ref
        ref=$1
        for k in $(echo "${!ref[@]}")
        do
            printf '{"name":"%s", "value":"%s"}\n' "$k" "${ref[$k]}"
        done | jq -s 'reduce .[] as $i ({}; .[$i.name] = $i.value)'
    }
    
    #-------------------------------------------------------------------------
    
    # print the grep output in key : value format
    function list_grep()
    {
        local listfile=$1
        local dir=$2
        eval $(jq -Rsr 'split("\n") | map(select(length > 0)) | reduce .[] as $line ([]; . + ["echo \($line) :; grep -rl \($line) \($dir); echo"]) | (join("; "))' --arg dir "$dir" < "$listfile")
    }
    
    #-------------------------------------------------------------------------
    
    # print the grep output as JSON dictionary
    function dict_grep()
    {
        local listfile=$1
        local dir=$2
        eval declare -A Arr=\($(eval echo $(jq -Rrs 'split("\n") | map(select(length > 0)) | reduce .[] as $k ([]; . + ["[\($k)]=\\\"$(grep -rl \($k) tmp)\\\""]) | (join(" "))' --arg dir $dir < tmp/list.txt))\)
        print_dict Arr
    }
    
    #-------------------------------------------------------------------------
    
    # call:
    list_grep $listfile $dir
    dict_grep $listfile $dir
    

    -喜满树

    【讨论】:

    • 是的,这就是我的意思,我的描述上的符号错误。但结果是一样的,它只打印文件的最后一行..
    • 哦,好吧,追加&gt;&gt; 应该在那里。对不起
    • 谢谢,它在我的任期内有效。但是在结果之前找不到如何写查询词,我无法理解哪个结果对应哪个查询。你认为我需要为此编写一个循环吗?
    • 是的,最简单的实现只是循环,而且由于程序也是多对多映射,所以循环更可取。但是,在变量或管道中获得 grep say 的输出后,您可以使用 awk 尝试使用字典和/或 JSON。
    • 是的,你应得的。您谈论解决方案一侧的箭头吗?我点击了很多次,但一直为零,我是新来的,对选项不太了解。
    猜你喜欢
    • 2019-07-22
    • 2020-07-31
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多