[英]recursive find for specific text for last n lines
我想以遞歸的方式找到一些特定的文本,我已經閱讀了這個非常流行的答案:
grep -rnw '/path/to/somewhere/' -e 'pattern'
但這對我不起作用,因為我的文件很大,可能需要很長時間才能在整個文件中找到,所以我只想在最后N行中搜索以提高執行操作所需的性能和時間,例如例如:
在每個文件名匹配的字符串將被打印之后,此代碼將打印所有文件。 您可以使用參數來精確地實現所需的功能。 例如,如果將-print移至末尾,則僅會打印匹配的文件(但在匹配的字符串之后)
find /path/to/somewhere/ -type f -print -exec bash -c "cat -n {} | tail | grep -we pattern" \;
我將使用GNU的parallel
, bash
和tail
:
my_tail_grep() {
file="$1"
pattern="$2"
N="${3:-100}" # Optional -- defaults to 100
res=$(tail -n "$N" "$file" | grep -we "$pattern")
[[ -n "$res" ]] && echo -e "${file}\n${res}" || :
}
export -f my_tail_grep # Necessary for parallel
find /path -type f | parallel my_tail_grep {} "$pattern" 100
使用find
命令通過n
參數指定最后N行 :
n=11
find /path/to/somewhere/ -type f -exec sh -c 'tail -n '$n' {} | grep -wn pattern' \; -print
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.