标签: grep

如何命令grep不显示搜索的字符串?

我想打印文件中一行的一部分。整条线看起来像这样。

Path=fy2tbaj8.default-1404984419419
Run Code Online (Sandbox Code Playgroud)

我只想打印Path=. 我试过了grep Path filename | head -5。但它不起作用。它仍然显示整行。我怎样才能做到这一点?

command-line scripts grep

15
推荐指数
5
解决办法
4万
查看次数

两个大文件的区别

我有“test1.csv”,它包含

200,400,600,800
100,300,500,700
50,25,125,310
Run Code Online (Sandbox Code Playgroud)

和 test2.csv ,它包含

100,4,2,1,7
200,400,600,800
21,22,23,24,25
50,25,125,310
50,25,700,5
Run Code Online (Sandbox Code Playgroud)

现在

diff test2.csv test1.csv > result.csv
Run Code Online (Sandbox Code Playgroud)

不同于

diff test1.csv test2.csv > result.csv
Run Code Online (Sandbox Code Playgroud)

我不知道哪个是正确的顺序,但我想要别的东西,上面的两个命令都会输出类似的东西

2 > 100,4,2,1,7
   3 2,3c3,5
   4 < 100,300,500,700
   5 < 50,25,125,310
   6 \ No newline at end of file
   7 ---
   8 > 21,22,23,24,25
   9 > 50,25,125,310
Run Code Online (Sandbox Code Playgroud)

我只想输出差异,因此 results.csv 应该是这样的

100,300,500,700
100,4,2,1,7
21,22,23,24,25
50,25,700,5
Run Code Online (Sandbox Code Playgroud)

我试过了diff -qdiff -s但他们没有成功。顺序无关紧要,重要的是我只想看到差异,没有 > 也没有 < 也没有空格。

grep -FvF 在较小的文件上而不是在大文件上做了技巧

第一个文件包含超过 500 万行,第二个文件包含 1300。

所以 results.csv 应该导致 …

grep diff sed awk

15
推荐指数
2
解决办法
6865
查看次数

Grep:星号 (*) 并不总是有效

如果我 grep 包含以下内容的文档:

ThisExampleString

...对于表达式This*Stringor *String,不返回任何内容。但是,This*按预期返回上述行。

表达式是否用引号括起来没有区别。

我认为星号表示任意数量的未知字符?为什么它只有在表达式的开头才有效?如果这是预期的行为,我应该使用什么来代替表达式This*String*String

command-line bash regex grep

15
推荐指数
3
解决办法
3万
查看次数

拖尾两个日志文件

我有一个 Web 应用程序,它输出到许多带有性能信息的日志文件。一个日志文件输出代码执行时间,另一个输出 SQL 计时。我无法控制记录器或生成日志文件的代码,但我想在一个地方输出日志。

目前我正在做这样的事情

tail -f sqlLogs.log | grep sql-time
tail -f perflogs.log | grep exec-time
Run Code Online (Sandbox Code Playgroud)

每次在应用程序中执行 SQL 时,都会向控制台输出一些内容。但是我必须在两个单独的 SSH 会话中运行代码。但是,我希望能够在同一个 SSH 会话中尾随两个文件。这可能吗?

files tail grep

14
推荐指数
1
解决办法
5536
查看次数

grep 列出每个文件一次

我正在查看文件中的一些文本,但是三个是很多文件,并且搜索文本在一个文件中出现多次,如何接收包含搜索文本的文件列表,并且每个文件只列出一次?

command-line search grep

14
推荐指数
1
解决办法
2万
查看次数

为什么我的 grep + regex 不起作用?

我最近决定足够了——我将学习流利地使用 grep。三个小时过去了,我已经被这个玩具问题难住了。

我目前正在同步一个 RAID5 阵列,可以通过读取/proc/mdstat. 的输出cat /proc/mdstat如下所示。

$ cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md1 : active raid5 sda4[0] sdb4[1] sdc4[2]
      5858765824 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
      [=============>.......]  resync = 67.3% (1972073120/2929382912) finish=205.7min speed=77537K/sec

md0 : active raid5 sda3[0] sdb3[1] sdc3[2]
      998400 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>
Run Code Online (Sandbox Code Playgroud)

为了好玩,我想我会使用实时watch监控/proc/mdstat,将它的输出通过管道传输到 grep,并仅显示估计的剩余时间。

我的方法如下:

watch …

grep

13
推荐指数
2
解决办法
3万
查看次数

高效搜索排序文件

我有一个大文件,每一行都包含一个字符串。我希望能够快速确定文件中是否有字符串。理想情况下,这将使用二进制斩波类型算法来完成。

一些谷歌搜索揭示了look带有-b标志的命令,该标志承诺使用二进制搜索算法定位和输出以给定前缀开头的所有字符串。不幸的是,它似乎无法正常工作,并为我知道在文件中的字符串返回空结果(它们由等效grep搜索正确返回)。

有谁知道有效搜索此文件的另一种实用程序或策略?

command-line grep text-processing

13
推荐指数
1
解决办法
6427
查看次数

grep - 显示线条直到特定模式

我在 Ubuntu 16.04 上,我想搜索文件,让我们说“if”,我想输出直到“endif”。该文件可能如下所示

if …
  SOME CODE
endif
Run Code Online (Sandbox Code Playgroud)

我可以用grep -A9 if my_file | grep -B9 endif. 如果“if”子句大于 9,并且多个“if”子句在同一个文件中,并且第一个 grep 命令包含多个“if”子句,则这不起作用。-m1第二个 grep 中的选项没有帮助。可以忽略嵌套的“if”子句。有人有想法,也许不是用grep?

如何为多行上的多个模式 grep 的区别

  1. 该问题要求一个解决方案,grep该解决方案已在问题中得到回答:grep -A9 if my_file | grep -B9 endif。该解决方案在我的情况下不起作用,但在另一个问题的情况下有效。

  2. 另一个问题的建议 grep 解决方案不起作用(使用 Ubuntu?):grep: ein nicht geschütztes ^ oder $ wird mit -Pz nicht unterstützt.类似于grep: a not protected ^ or $ is not supported with -Pz. 我使用以下内容:

    root:/tmp# cat file
    Some …
    Run Code Online (Sandbox Code Playgroud)

command-line grep

13
推荐指数
2
解决办法
3万
查看次数

Why the pipe command "l | grep "1" " get the wrong result?

As the picture illustrates, I use l to get the file in the current folder. And then I want to get the file with number 1, so I use the pipe and the grep.

But why the 2 and 22 file show out? And what is the 1;34m?

$ l
./ ../ 1 11 2 22
$ l | grep "1"
1;34m./ 1;32m../ 1 11 2 22
Run Code Online (Sandbox Code Playgroud)

Update

I have already alias the command l in my …

command-line grep ls pipe

13
推荐指数
2
解决办法
2569
查看次数

在匹配前显示所有行

我想在匹配之前显示所有行,例如不仅是 10、7 或 14 行,如如何在 bash 中的 grep 结果之前/之后获取行中所述?.

我该怎么做?是否包含匹配的行并不重要。

例如,而不是:

... | grep -B 10 -- "foo"
Run Code Online (Sandbox Code Playgroud)

我想要:

... | grep -B -- "foo"
Run Code Online (Sandbox Code Playgroud)

但是这最后的代码不起作用。

command-line grep text-processing

12
推荐指数
3
解决办法
3221
查看次数

标签 统计

grep ×10

command-line ×7

text-processing ×2

awk ×1

bash ×1

diff ×1

files ×1

ls ×1

pipe ×1

regex ×1

scripts ×1

search ×1

sed ×1

tail ×1