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

发布于 2024-08-17 04:32:46 字数 108 浏览 9 评论 0原文

我想对通常有很长行的 HTML 文件运行 ack 或 grep。我不想看到很长的线反复换行。但我确实想查看包围与正则表达式匹配的字符串的长行的一部分。我怎样才能使用 Unix 工具的任意组合来获得这个?

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 a long line that surrounds a string that matches the regular expression. How can I get this using any combination of Unix tools?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

贵在坚持 2024-08-24 04:32:46

您可以使用 grep 选项 -oE,可能与将模式更改为 ".{0,10}.{0,10}" 结合使用为了查看周围的一些上下文:

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

       -E, --extended-regexp
             Interpret pattern as an extended regular expression (i.e., force grep to behave as egrep).

例如(来自@Renaud的评论):

grep -oE ".{0,10}mysearchstring.{0,10}" myfile.txt

或者,您可以尝试 -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.

You could use the grep options -oE, possibly in combination with changing your pattern to ".{0,10}<original pattern>.{0,10}" in order to see some context around it:

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

       -E, --extended-regexp
             Interpret pattern as an extended regular expression (i.e., force grep to behave as egrep).

For example (from @Renaud's comment):

grep -oE ".{0,10}mysearchstring.{0,10}" myfile.txt

Alternatively, you could try -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.
胡大本事 2024-08-24 04:32:46

通过 cut 传送结果。我还在考虑添加一个 --cut 开关,这样你就可以说 --cut=80 并且只获得 80 列。

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

╰ゝ天使的微笑 2024-08-24 04:32:46

您可以使用 less 作为寻呼机来进行 ack 和截断长行: ack --pager="less -S" 这会保留长行,但将其保留在一行上而不是换行。要查看该行的更多内容,请使用箭头键向左/向右滚动。

我有以下别名设置来执行此操作:

alias ick='ack -i --pager="less -R -S"' 

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"' 
Saygoodbye 2024-08-24 04:32:46

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

在无法使用 -E 的异常情况下,请使用小写 -e 代替。

说明:

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

In the unusual situation where you cannot use -E, use lowercase -e instead.

Explanation:
illustrative command explanation

枉心 2024-08-24 04:32:46

要获取从 1 到 100 的字符。

cut -c 1-100

您可能希望将范围基于当前终端,例如

cut -c 1-$(tput cols)

To get characters from 1 to 100.

cut -c 1-100

You might want to base the range off the current terminal, e.g.

cut -c 1-$(tput cols)
稳稳的幸福 2024-08-24 04:32:46

我将以下内容放入 .bashrc 中:

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

然后,您可以在命令行上使用 grepl 以及可用于 grep 的任何参数。使用箭头键查看较长线条的尾部。使用q退出。

说明:

  • grepl() {:定义一个新函数,该函数将在每个(新)bash 控制台中可用。
  • $(which grep):获取grep的完整路径。 (Ubuntu 为 grep 定义了一个别名,它相当于 grep --color=auto。我们不需要那个别名,而是原来的 grep .)
  • --color=always:对输出进行着色。 (别名中的 --color=auto 不起作用,因为 grep 检测到输出被放入管道中,然后不会对其进行着色。)
  • $@:将提供给 grepl 函数的所有参数放在这里。
  • less:使用less显示行
  • -R:显示颜色
  • S:不要打断长行

I put the following into my .bashrc:

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

You can then use grepl on the command line with any arguments that are available for grep. Use the arrow keys to see the tail of longer lines. Use q to quit.

Explanation:

  • grepl() {: Define a new function that will be available in every (new) bash console.
  • $(which grep): Get the full path of grep. (Ubuntu defines an alias for grep that is equivalent to grep --color=auto. We don't want that alias but the original grep.)
  • --color=always: Colorize the output. (--color=auto from the alias won't work since grep detects that the output is put into a pipe and won't color it then.)
  • $@: Put all arguments given to the grepl function here.
  • less: Display the lines using less
  • -R: Show colors
  • S: Don't break long lines
当爱已成负担 2024-08-24 04:32:46

摘自:http://www.topbug.ne​​t/blog/2016/08/18/truncate-long-matching-lines-of-grep-a-solution-that-preserves-color/

建议的方法 ".{0,10}.{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 模式 file_with_long_lines 应该显示匹配的行,但带有匹配字符串周围只有 10 个字符。

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.

忆梦 2024-08-24 04:32:46

Silver Searcher (ag) 通过 --width NUM 原生支持选项。它将用 [...] 替换其余较长的行。

示例(在 120 个字符后截断):

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

在 ack3 中,计划有一个类似功能,但目前没有实施。

The Silver Searcher (ag) supports its natively via the --width NUM option. It will replace the rest of longer lines by [...].

Example (truncate after 120 characters):

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

In ack3, a similar feature is planned but currently not implemented.

少跟Wǒ拽 2024-08-24 04:32:46

这就是我所做的:

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

在我的 .bash_profile 中,我覆盖 grep,以便它自动运行tput rmam 之前和 tput smam 之后,禁用换行,然后重新启用它。

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.

鸠书 2024-08-24 04:32:46

如果您愿意,ag 也可以采用正则表达式技巧:

ag --column -o ".{0,20}error.{0,20}"

ag can also take the regex trick, if you prefer it:

ag --column -o ".{0,20}error.{0,20}"
乖乖哒 2024-08-24 04:32:46

bgrep 如果行不一定适合内存

grep 仅当行适合内存时才有效,但 bgrep 也适用于不适合内存的大行't。

我时不时地回到这个随机仓库: https://github.com/tmbinc/bgrep 安装:

curl -L 'https://github.com/tmbinc/bgrep/raw/master/bgrep.c' | gcc -O2 -x c -o $HOME/.local/bin/bgrep -

使用:

bgrep `printf %s saf | od -t x1 -An -v | tr -d '\n '` myfile.bin

示例输出:

myfile.bin: c80000003
\x02abc
myfile.bin: c80000007
dabc

我已经在不适合内存的文件上对其进行了测试,并且效果很好。

我在以下位置提供了更多详细信息:https: //unix.stackexchange.com/questions/223078/best-way-grep-a-big-binary-file/758528#758528

bgrep if lines don't necessarily fit into memory

grep only works if the lines fit into memory, but bgrep also works on huge lines that don't.

I keep coming back to this random repo from time to time: https://github.com/tmbinc/bgrep Install:

curl -L 'https://github.com/tmbinc/bgrep/raw/master/bgrep.c' | gcc -O2 -x c -o $HOME/.local/bin/bgrep -

Use:

bgrep `printf %s saf | od -t x1 -An -v | tr -d '\n '` myfile.bin

Sample output:

myfile.bin: c80000003
\x02abc
myfile.bin: c80000007
dabc

I have tested it on files that don't fit into memory, and it worked just fine.

I've given further details at: https://unix.stackexchange.com/questions/223078/best-way-to-grep-a-big-binary-file/758528#758528

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文