how to find a file name that contains a string “abcd ” in the file content from multiple files in the directory using awk

前端 未结 2 1501
情书的邮戳
情书的邮戳 2020-12-22 05:13

the \"bookrecords\" directory has multiple files

bookrecords
           1.txt
           2.txt 
           3.txt .....

file 2.txt has the c

相关标签:
2条回答
  • 2020-12-22 05:51

    If you have a deep directory hierarchy below bookrecords, you could do a

    grep -Elsrx '[[:space:]]*author:[[:space:]]*abcd[[:space:]]*' bookrecords
    

    If you only want to look at *.txt files inside bookrecords, do a

    grep -Elx '[[:space:]]*author:[[:space:]]*abcd[[:space:]]*' bookrecords/*.txt
    

    The -l produces only the file names in the output, and -x takes care that the regular expression matches the whole line. Hence, a line containing myauthor:abcde would not be selected. Adapt the regexp to your taste, and be careful when replacing abcd by a real author name (it should not contain unescaped regexp characters, such as for instance a period).

    If you want to start the line with at least one white space, and have at least one space after the colon:

    grep -Elsrx '[[:space:]]+author:[[:space:]]+abcd[[:space:]]*' bookrecords
    
    0 讨论(0)
  • 2020-12-22 05:57

    with awk

    $ awk -v search_string="$name" '$0~search_string{print FILENAME; exit}' bookrecords/*
    

    however, I think grep is better if you're not structurally searching

    $ grep -lF "$name" bookrecords/*
    
    0 讨论(0)
提交回复
热议问题