Linux grep 限制字符 - 一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10487257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep limited characters - one line
提问by Patrick Maciel
I want to look up a word in multiple files, and return only a single line per result, or a limited number of characters (40 ~ 80 characters for example), and not the entire line, as by default.
我想在多个文件中查找一个单词,并且每个结果只返回一行,或者有限数量的字符(例如 40 ~ 80 个字符),而不是默认情况下的整行。
grep -sR 'wp-content' .
file_1.sql:3309:blog/wp-content
file_1.sql:3509:blog/wp-content
file_2.sql:309:blog/wp-content
Currently I see the following:
目前我看到以下内容:
grep -sR 'wp-content' .
file_1.sql:3309:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without.
file_1.sql:3509:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without.
file_2.sql:309:blog/wp-content-Progressively predominate impactful systems without resource-leveling best practices. Uniquely maximize virtual channels and inexpensive results. Uniquely procrastinate multifunctional leadership skills without visionary systems. Continually redefine prospective deliverables without.
采纳答案by user unknown
egrep -Rso '.{0,40}wp-content.{0,40}' *.sh
This will not call the Radio-Symphonie-Orchestra, but -o(nly matching).
这不会调用 Radio-Symphonie-Orchestra,而是调用 -o(仅匹配)。
A maximum of 40 characters before and behind your pattern. Note: *e*grep.
模式前后最多 40 个字符。注意:* e*grep。
回答by Tim Pote
If you change the regex to '^.*wp-content'
you can use egrep -o
. For example,
如果您将正则表达式更改为'^.*wp-content'
可以使用egrep -o
. 例如,
egrep -sRo '^.*wp-content' .
The -o
flag make egrep only print out the portion of the line that matches. So matching from the start of line to wp-content
should yield the sample output in your first code block.
该-o
标志使 egrep 仅打印出匹配的行部分。因此,从行首到匹配wp-content
应该会在您的第一个代码块中产生示例输出。
回答by aemus
You could use a combination of grep and cut
您可以使用 grep 和 cut 的组合
Using your example I would use:
使用您的示例,我将使用:
grep -sRn 'wp-content' .|cut -c -40
grep -sRn 'wp-content' .|cut -c -80
That would give you the first 40 or 80 characters respectively.
这将分别为您提供前 40 或 80 个字符。
edit:
编辑:
Also, theres a flag in grep, that you could use:
此外,grep 中有一个标志,您可以使用:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.
This with a combination of what I previously wrote:
这结合了我之前写的内容:
grep -sRnm 1 'wp-content' .|cut -c -40
grep -sRnm 1 'wp-content' .|cut -c -80
That should give you the first time it appears per file, and only the first 40 or 80 chars.
这应该让您第一次出现在每个文件中,并且只有前 40 或 80 个字符。