how to make 'ack' command print the filename that has found the result on every line

1

1

By default ack command prints the output in follwing way

ack --jsp '</div>'

a.jsp
5:</span></div>
10:</table></div>
b.jsp
3:</div><div>xyz
8:</div><a>

is there any way to output the result in following way ?

<filename>:<linenum>:<matchedtext>   

a.jsp:5:</div>
a.jsp:10:</div>
b.jsp:3:</div>
b.jsp:8:</div>

Though there is --output=expr to output match based on regex capture group I am not sure if there is any way to get the name of the file that has matched text along with the line number

ack --jsp -o -h --output=':$1:$2' '(</(div)>)'

:</div>:div
:</div>:div
:</div>:div
:</div>:div

if not possible with ack, is there any alternate way to do it ?

Prashant Bhate

Posted 2012-06-14T21:11:18.057

Reputation: 775

Answers

2

Ok a quick scan through ack source I found following command line option to get required result

ack --jsp --nogroup -o '(</(div)>)'


a.jsp:5:</div>
a.jsp:10:</div>
b.jsp:3:</div>
b.jsp:8:</div>

or

ack --jsp --nogroup --output='$2' '(</(div)>)'


a.jsp:5:div
a.jsp:10:div
b.jsp:3:div
b.jsp:8:div

Prashant Bhate

Posted 2012-06-14T21:11:18.057

Reputation: 775