linux:在每个输出行的末尾打印字符串

linux:在每个输出行的末尾打印字符串,linux,solaris,Linux,Solaris,我执行下面的脚本 grep -i 'Exception' Exceptions.log | sort | uniq -c | sort -n 并获得以下结果(每次结果集将不同) 实际上,我还想在每行的开头或结尾打印文件名 6 java.lang.NullPointerException Exceptions 48 java.sql.SQLException Exceptions 64 excep.NotFoundException Exceptions 671 exception.Par

我执行下面的脚本

grep -i 'Exception' Exceptions.log | 
sort | 
uniq -c | 
sort -n
并获得以下结果(每次结果集将不同)

实际上,我还想在每行的开头或结尾打印文件名

6 java.lang.NullPointerException Exceptions
48 java.sql.SQLException Exceptions
64 excep.NotFoundException Exceptions
671 exception.ParseErrorException Exceptions
690 Exception Exceptions


请帮我实现这个目标。

你可以使用
-H
选项,例如
grep-H


查看页面了解更多选项

似乎最容易将
..sed-e的/^/Exceptions.log/'
添加到管道中。但你可以这样做:

awk '/Exception/ {a[$0 " " FILENAME]++} 
    END {for(i in a) print a[i], i}' Exceptions.log | sort -n

这将允许您轻松搜索多个文件。(例如,只需将Exceptions.log替换为
*

为什么不添加字符串
Exceptions.log
,因为这似乎是文件名?
Exceptions 6 java.lang.NullPointerException
Exceptions 48 java.sql.SQLException
Exceptions 64 excep.NotFoundException
Exceptions 671 exception.ParseErrorException
Exceptions 690 Exception
   -H, --with-filename
          Print the file name for each match.  This is  the  default  when
          there is more than one file to search.
awk '/Exception/ {a[$0 " " FILENAME]++} 
    END {for(i in a) print a[i], i}' Exceptions.log | sort -n