簡體   English   中英

Unix-按順序打印grep輸出

[英]Unix - print grep output in order

我正在使用此指令:

VAR=$(grep -r -i --color=always --exclude=\*.{sh,pdf,doc,docx} "$WORD" "$DIRECTORY")

這給了我這樣的輸出:

/dir1NameWithSpaces/File1_05012004/file6.sml:file6 content .... with words and symbols
/dir1NameWithSpaces/File1_05012004/fil3.txt:file3 content .... with words and symbols
/dir1NameWithSpaces/File1_04092008/file.txt:file content .... with words and symbols

我需要提取每一行(例如,使用循環),僅在1行中從grep打印文件名,並在另一行上打印grep命令的內容。

為了每次在多文件內容中進行搜索時都必須使用它,因此必須具有通用性。

此外,如果您正在考慮直接使用grep,則具有內容的行不必具有文件名,因此我只想保留“:”符號。

這里要澄清的是一個示例:

/dir1NameWithSpaces/File1_05012004/file6.sml:
:file6 content .... with words and symbols
/dir1NameWithSpaces/File1_05012004/fil3.txt:
file3 content .... with words and symbols
/dir1NameWithSpaces/File1_04092008/file.txt:
file content .... with words and symbols

換句話說,我的grep的輸出必須經過整齊地打印。

謝謝

$ echo foo | tee bar.txt baz.txt qux.txt
foo

$ grep -r foo . | sed 'y/:/\n/'
./bar.txt
foo
./baz.txt
foo
./qux.txt
foo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM