简体   繁体   English

如何打印匹配grep模式的文件的文件详细信息

[英]How to print file details of files matching grep pattern

I want to print file details along with grep output but unable to do so. 我想打印文件详细信息以及grep输出但无法这样做。 Eg, for the command 例如,对于命令

grep 3456 A.txt

I get the output 我得到了输出

A.txt

but would like the output 但是想要输出

-rw-rw-r-- 1 tarun tarun   41356911 Aug 25 01:31 A.txt

I tried the following without success: 我尝试了以下但没有成功:

  1. grep 34567 A.txt | xargs ls -tlr

  2. grep 34567 A.txt | while read line ; do echo "$line" | date %s.%N ; done

  3. grep -Hr 34567 A.txt | awk -F: '{"stat -c %z "$1 | getline r; print r": "$0 }'

  4. grep 34567 A.txt | awk -F: '{"date -r \\""$1"\\" +\\"%F %R\\"" | getline d; print d,$0}'

grep -Zl 3456 * | xargs -0 ls -l

with GNU grep. 用GNU grep。 The options are: 选项是:

  • grep -Z and xargs -0 : separate output names by a NULL byte instead of by whitespace. grep -Zxargs -0 :用NULL字节而不是空格分隔输出名称。 This way you can handle filenames that include spaces. 这样您就可以处理包含空格的文件名。
  • grep -l : print only the filenames that match grep -l :只打印匹配的文件名
  • ls -l : Standard ls long output, which appears to be what you are asking for. ls -l :标准ls长输出,这似乎是你要求的。

Tested on latest cygwin. 测试了最新的cygwin。

There isn't need for xargs here; 这里不需要xargs ; find can be used to both run grep and run ls , or even to emit ls -style output itself. find既可用于运行grep ,也可用于运行ls ,甚至可用于发出ls style输出本身。

find . -maxdepth 1 -type f \
       -exec grep -q -e '1234567' -- '{}' ';' \
       -exec ls -l {} +

...or, even better: ......或者,甚至更好:

find . -maxdepth 1 -type f \
       -exec grep -q -e '1234567' -- '{}' ';' \
       -ls

The -ls action uses an output format akin to ls -l . -ls动作使用类似于ls -l的输出格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在txt文件中搜索模式,并在与第一个模式匹配的文件上打印第二个模式匹配的结果? - How to search for a pattern in a txt file and print the result of a second pattern matching on the files matching the first pattern? 如何使用grep模式匹配来迭代移动文件/文件夹 - how to iteratively move files/folder using grep pattern matching 如何在特征码文件上调用grep? - How to call grep on pattern files? 文件中的grep模式,打印模式而不是匹配的字符串 - grep pattern from file, print the pattern instead matched string 如何在文件的前两行以外的所有行中grep匹配模式 - How to grep a matching pattern in all the lines in a file except the first two lines 使用bash读取json文件并从匹配的模式中grep字符串 - Read a json file and grep a string from matching pattern using bash 打印文件中匹配模式的所有实例 - Print all the instances of a matching pattern in a file 如何从 a.csv 文件中提取 grep 列及其相邻的文本行,然后以特定模式打印以实现 zenity? - How to grep columns and their ajoining rows of text from a .csv file then print in a specific pattern for zenity? 匹配模式并使用awk或grep打印文件中的相应列 - match pattern and print corresponding columns from a file using awk or grep 删除与 grep 模式匹配的行 - Remove lines matching a grep pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM