How to trim long matching lines returned by grep or ack

I want to run ack or grep in HTML files that often have very long lines. I do not want to see very long lines that are repeated multiple times. But I want to see only that part of the long string that surrounds the string that matches the regular expression. How can I get this using any combination of Unix tools?

+54
unix grep ack
Jan 9 '10 at 20:19
source share
4 answers

You can use the grep -o option, possibly in combination with changing the pattern to ".{0,10}<original pattern>.{0,10}" to see some context around it:

        -o, --only-matching
               Show only the part of a matching line that matches PATTERN.

.. or -c :

        -c, --count
               Suppress normal output;  instead print a count of matching lines
               for each input file.  With the -v, --invert-match option (see
               below), count non-matching lines.
+54
Jan 9 '10 at 20:21
source share

Produce your results through cut . I am also considering adding a -cut switch so that you can say --cut = 80 and get only 80 columns.

+27
Jan 09 '10 at 21:19
source share

You can use less as a pager for ack and cut long lines: ack --pager="less -S" This saves a long line, but leaves it on one line instead of wrapping. To see more lines, scroll left / right less with the arrow keys.

I have the following alias setting for ack:

 alias ick='ack -i --pager="less -R -S"' 
+17
Jun 14 2018-12-18T00:
source share

Taken from: http://www.topbug.net/blog/2016/08/18/truncate-long-matching-lines-of-grep-a-solution-that-preserves-color/

The proposed approach ".{0,10}<original pattern>.{0,10}" works fine, except that the color of the backlight is often confused. I created a script with a similar result, but the color is also saved:

 #!/bin/bash # Usage: # grepl PATTERN [FILE] # how many characters around the searching keyword should be shown? context_length=10 # What is the length of the control character for the color before and after the # matching string? # This is mostly determined by the environmental variable GREP_COLORS. control_length_before=$(($(echo a | grep --color=always a | cut -da -f '1' | wc -c)-1)) control_length_after=$(($(echo a | grep --color=always a | cut -da -f '2' | wc -c)-1)) grep -E --color=always "$1" $2 | grep --color=none -oE \ ".{0,$(($control_length_before + $context_length))}$1.{0,$(($control_length_after + $context_length))}" 

Assuming the script is saved as grepl , then grepl pattern file_with_long_lines should display the corresponding lines, but only 10 characters around the corresponding line.

+1
Aug 19 '16 at 1:51 on
source share



All Articles