Grep --byte-offset not returning the offset (Grep version 2.5.1)

六月ゝ 毕业季﹏ 提交于 2019-12-23 12:17:56

问题


Grep --byte-offset not returning the offset (Grep version 2.5.1) Hi, I am trying to get the position of a repeated string in a line using

Code:

grep -b -o "pattern"

In my server I am using GNU grep version 2.14 and the code is working fine. However when I am deploying the same code in a different server which is using GNU grep version 2.5.1 the code is not working properly. Even though the byte offset option is available there. Any idea how to solve it.

Example:

Code:

export string="abc cat mat rat cat bat cat fat rat tat tat cat"
echo $string|grep -b -o "cat"

Expected output (and supported in grep 2.14):

4:cat
16:cat
24:cat
44:cat

But same code with grep version 2.5.1 is giving the following output:

0:cat
cat
cat
cat

Please suggest..


回答1:


It was a bug in grep as some notes in its Changelog refers to it:

    * src/grep.c (nlscan): Make this function more robust by removing
      the undocumented assumption that its "lim" argument points
      right after a line boundary.  This will be used later to fix
      --byte-offset's broken behavior.  Patch #3769.

Use later versions at least 2.5.3 where it seems fixed already.



来源:https://stackoverflow.com/questions/18802707/grep-byte-offset-not-returning-the-offset-grep-version-2-5-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!