【问题标题】:Unix - print grep output in orderUnix - 按顺序打印 grep 输出
【发布时间】: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 的输出必须按部就班地打印出来。

谢谢

【问题讨论】:

  • $() 构造中是否缺少结束 )
  • 当您想要格式化输出时,切换到awkfind 结合awk 很容易控制和定制
  • 是的,谢谢,你是对的
  • 如果你把输出放到环境变量中,使用--color=always是没有意义的。

标签: linux unix grep output


【解决方案1】:
$ 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

【讨论】:

    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 2014-03-21
    • 2019-10-31
    • 1970-01-01
    • 2019-06-27
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多