How to truncate long matching lines returned by grep or ack

前端 未结 8 1631
攒了一身酷
攒了一身酷 2020-12-13 05:38

I want to run ack or grep on HTML files that often have very long lines. I don\'t want to see very long lines that wrap repeatedly. But I do want to see just that portion of

相关标签:
8条回答
  • 2020-12-13 05:50

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

    The suggested approach ".{0,10}<original pattern>.{0,10}" is perfectly good except for that the highlighting color is often messed up. I've created a script with a similar output but the color is also preserved:

    #!/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 -d a -f '1' | wc -c)-1))
    control_length_after=$(($(echo a | grep --color=always a | cut -d a -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 matching lines but with only 10 characters around the matching string.

    0 讨论(0)
  • 2020-12-13 05:52
    cut -c 1-100
    

    gets characters from 1 to 100.

    0 讨论(0)
  • 2020-12-13 05:54

    In the unusual situation where you cannot use -E, you can use:

    grep -oe ".\{0,10\}error.\{0,10\}" mylogfile.txt
    
    0 讨论(0)
  • 2020-12-13 05:57

    Pipe your results thru cut. I'm also considering adding a --cut switch so you could say --cut=80 and only get 80 columns.

    0 讨论(0)
  • 2020-12-13 05:57

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

    I have the following alias setup for ack to do this:

    alias ick='ack -i --pager="less -R -S"' 
    
    0 讨论(0)
  • 2020-12-13 05:58

    Here's what I do:

    function grep () {
      tput rmam;
      command grep "$@";
      tput smam;
    }
    

    In my .bash_profile, I override grep so that it automatically runs tput rmam before and tput smam after, which disabled wrapping and then re-enables it.

    0 讨论(0)
提交回复
热议问题