![](/img/trans.png)
[英]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?
[英]How to print file details of files matching grep pattern
我想打印文件詳細信息以及grep輸出但無法這樣做。 例如,對於命令
grep 3456 A.txt
我得到了輸出
A.txt
但是想要輸出
-rw-rw-r-- 1 tarun tarun 41356911 Aug 25 01:31 A.txt
我嘗試了以下但沒有成功:
grep 34567 A.txt | xargs ls -tlr
grep 34567 A.txt | while read line ; do echo "$line" | date %s.%N ; done
grep -Hr 34567 A.txt | awk -F: '{"stat -c %z "$1 | getline r; print r": "$0 }'
grep 34567 A.txt | awk -F: '{"date -r \\""$1"\\" +\\"%F %R\\"" | getline d; print d,$0}'
grep -Zl 3456 * | xargs -0 ls -l
用GNU grep。 選項是:
grep -Z
和xargs -0
:用NULL字節而不是空格分隔輸出名稱。 這樣您就可以處理包含空格的文件名。 grep -l
:只打印匹配的文件名 ls -l
:標准ls
長輸出,這似乎是你要求的。 測試了最新的cygwin。
這里不需要xargs
; find
既可用於運行grep
,也可用於運行ls
,甚至可用於發出ls
style輸出本身。
find . -maxdepth 1 -type f \
-exec grep -q -e '1234567' -- '{}' ';' \
-exec ls -l {} +
......或者,甚至更好:
find . -maxdepth 1 -type f \
-exec grep -q -e '1234567' -- '{}' ';' \
-ls
-ls
動作使用類似於ls -l
的輸出格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.