【发布时间】:2013-05-05 16:45:53
【问题描述】:
我正在使用这个指令:
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 的输出必须按部就班地打印出来。
谢谢
【问题讨论】:
-
$()构造中是否缺少结束)? -
当您想要格式化输出时,切换到
awk。find结合awk很容易控制和定制 -
是的,谢谢,你是对的
-
如果你把输出放到环境变量中,使用
--color=always是没有意义的。