我有以下要求,在目录中我有一个文件列表,我必须在每个文件中找到一个唯一字符串的起始索引。这个字符串对每个文件都是通用的。如果我在shell脚本中获得解决方案会更好。
是文件列表,并且假设我想找到字符串“,hi,this,is,vijay,kumar”的起始索引,这将在每个文件中确定。
答案 0 :(得分:0)
让我们考虑一下这些测试文件:
$ cat file1
,hi,this,is,vijay,kumar
$ cat file2
Some text here,hi,this,is,vijay,kumar
$ cat file3
Some more text
here that goes
on,hi,this,is,vijay,kumar
要查找每个文件中字符串的位置(以字节为单位):
$ grep -ob ',hi,this,is,vijay,kumar' file*
file1:0:,hi,this,is,vijay,kumar
file2:14:,hi,this,is,vijay,kumar
file3:32:,hi,this,is,vijay,kumar
-b
告诉grep返回输入文件中基于0的字节偏移量。 -o
告诉grep返回匹配字符串本身的字节偏移量,而不是匹配字符串所在的行。
从结果中,我们看到你的字符串出现在file1的字节偏移量0,file2的字节偏移量14和file3的字节偏移量32处。
将file*
替换为您的文件名称,或者更简单地将任何与您感兴趣的文件名称匹配的glob替换。从您的图片中可能是201606231142_63*
。