Bash - ack - limit length of matched line
Asked Answered
D

3

6

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.

Discovert answered 13/5, 2012 at 2:6 Comment(1)
It would be helpful to know what pattern you're matching against, and what the relevant parts of the javascript file look like... I suspect that you're going to have more luck doing pattern matching rather than taking a fixed width substring.Discordancy
I
7

You can:

  • use -o option to Show only the part of a line matching PATTERN
  • use -l option to Only print filenames containing matches
Illuminate answered 14/5, 2012 at 0:23 Comment(4)
Hmm, neither are really that helpful, I still want to see the context around a matchDiscovert
Modify your pattern to -o '.{0,24}pattern.{0,24}' where 24 is the length of the context you want, and pattern is your original pattern. You could embed this in a script.Armitage
Ah, I see. Close to what I want. I'll see if I can modify the vim script to re-add highlighting after the factDiscovert
I liked @tripleee's solution but note that it made my searching dramatically slower, even for 24 characters!Asclepiadaceous
E
2

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

Echikson answered 5/6, 2012 at 17:16 Comment(0)
M
2

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).

Maurizia answered 25/9, 2012 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.