Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - ack - limit length of matched line

Tags:

bash

args

limit

ack

Is there a way to limit the length of the line matched by ack? If ack matches, for example, a minified javascript file, the line it prints out can be very large. I still want it to show the highlighted match, but ideally with the line cut before and after the match with elipses: "... stuff match stuff ...". Not seeing anything conclusive in the ack docs.

I tried digging into it with something like

ack pattern | awk '{print substr($0,1,200) }'

but awk seems to be stripping coloring and formatting, which is undesired. Also I tried (bear with me for a second):

ack pattern | ack pattern

To see if I could re-ack on each line and chop it up or something, but that has the same formatting problem. Piping ack output removes the line breaks apparently?

Edited based on comment:

Maybe it will help to explain the main reason why I want this. I use ack.vim to search in my project. I often want to ack through my javascript files to find a word, like "cookie". We have jQuery and other minified libraries versioned in our code, so they show up in the ack results. Since those files are minified, there are very few line breaks, and it shows a gigantic line in the split window which I have to scroll past. I almost never care about that match so it's annoying to take up so much space. I could just add those minified file names to my ackrc to ignore but I'd prefer not to have to add a new file name every time we add a minified library. There is an open issue on the ack.vim repo about this. I want to know if it's possible to do purely through bash-fu.

like image 211
Andy Ray Avatar asked Feb 21 '23 12:02

Andy Ray


2 Answers

You can:

  • use -o option to Show only the part of a line matching PATTERN
  • use -l option to Only print filenames containing matches
like image 53
kev Avatar answered Feb 27 '23 20:02

kev


If what you really want is to ignore minified Javascript files, then upgrade your ack. ack 1.96 and beyond ignores minified JavaScript files.

like image 42
Andy Lester Avatar answered Feb 27 '23 18:02

Andy Lester