سؤال

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.

هل كانت مفيدة؟

المحلول

You can:

  • use -o option to Show only the part of a line matching PATTERN
  • use -l option to Only print filenames containing matches

نصائح أخرى

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

ack cleverly disables color when it is being piped, but you can override that with --color:

ack --color foo | cut -c1-80 | sed "s/$/\e[0;30m/"

(the last pipe is to reset colors in case text is cut in the middle of selection)

There are similar options for formatting (--heading, --break).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top