代码之家  ›  专栏  ›  技术社区  ›  dan

如何截断grep或ack返回的长匹配行

  •  67
  • dan  · 技术社区  · 16 年前

    我想在通常有很长行的HTML文件上运行ack或grep。我不想看到很长的线反复缠绕。但我确实希望看到一条长线的那一部分,它围绕着一个与正则表达式匹配的字符串。我如何使用Unix工具的任意组合来实现这一点?

    5 回复  |  直到 9 年前
        1
  •  108
  •   Ether    16 年前

    您可以使用grep选项 -o ,可能与将您的模式更改为 ".{0,10}<original pattern>.{0,10}"

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

    -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.
    
        2
  •  51
  •   Ronan Boiteau    8 年前

    cut --cut 切换,这样你就可以说 --cut=80 只得到80列。

        3
  •  26
  •   Jonah Braun    14 年前

    您可以将less用作ack和chop长线的传呼机: ack --pager="less -S" 这将保留长线,但将其保留在一条线上,而不是环绕。要查看该行的更多内容,请使用箭头键向左/向右滚动至更少内容。

    我为ack设置了以下别名以执行此操作:

    alias ick='ack -i --pager="less -R -S"' 
    
        4
  •  8
  •   edib    8 年前
    cut -c 1-100
    

    获取从1到100的字符。

        5
  •  7
  •   Josh Withee    5 年前

    grep -oE ".\{0,10\}error.\{0,10\}" mylogfile.txt

    -E ,请使用小写字母 -e 相反

    说明: enter image description here

        6
  •  2
  •   Benjamin W.    9 年前

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

    建议的办法 ".{0,10}<original pattern>.{0,10}"

    #!/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))}"
    

    假设脚本另存为 grepl 那么 grepl pattern file_with_long_lines 应显示匹配行,但匹配字符串周围仅包含10个字符。

        7
  •  1
  •   pt1    6 年前

    我把以下内容放进我的 .bashrc

    grepl() {
        $(which grep) --color=always $@ | less -RS
    }
    

    然后您可以使用 grepl grep . 使用箭头键查看较长线条的尾部。使用 q

    说明:

    • grepl() { :定义将在每个(新)bash控制台中可用的新函数。
    • $(which grep) 格雷普 格雷普 grep --color=auto . 我们不想要那个别名,但要原件 .)
    • --color=always :为输出着色( --color=auto 从那以后,别名就不起作用了 格雷普
    • $@ :将所有给定的参数放入 在这里起作用。
    • less :使用显示行 较少的
    • -R :显示颜色
    • S
        8
  •  1
  •   ognockocaten    6 年前

    我是这样做的:

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

    override grep,使其自动运行 tput rmam 前后 tput smam

        9
  •  0
  •   Luke Miles    5 年前

    ag 如果您喜欢,也可以使用正则表达式技巧:

    ag --column -o ".{0,20}error.{0,20}"
    
        10
  •  0
  •   Philipp Claßen    5 年前

    The Silver Searcher (ag) 通过本地 --width NUM 选项它将取代其余的较长线路 [...] .

     $ ag --width 120 '@patternfly'
     ...
     1:{"version":3,"file":"react-icons.js","sources":["../../node_modules/@patternfly/ [...]
    

    similar feature is planned