Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String shell脚本中每个文件中唯一字符串的起始索引_String_Shell_Indexing_Scripting - Fatal编程技术网

String shell脚本中每个文件中唯一字符串的起始索引

String shell脚本中每个文件中唯一字符串的起始索引,string,shell,indexing,scripting,String,Shell,Indexing,Scripting,我有以下要求,在目录中我有一个文件列表,我必须在每个文件中找到唯一字符串的起始索引。这个字符串对每个文件都是通用的。如果我能在shell脚本中找到解决方案,那就更好了 是文件列表,假设我想找到字符串“开始”的索引“,Hi,HIS,IS,Vijayi,库马尔”,这将在每个文件中确定。 < P>让我们考虑这些测试文件: $ cat file1 ,hi,this,is,vijay,kumar $ cat file2 Some text here,hi,this,is,vijay,kumar $ ca

我有以下要求,在目录中我有一个文件列表,我必须在每个文件中找到唯一字符串的起始索引。这个字符串对每个文件都是通用的。如果我能在shell脚本中找到解决方案,那就更好了


是文件列表,假设我想找到字符串“开始”的索引“,Hi,HIS,IS,Vijayi,库马尔”,这将在每个文件中确定。

< P>让我们考虑这些测试文件:

$ 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。从你的形象来看,这可能是<代码> 201606121142Y63*<代码> < /P> < P>让我们考虑这些测试文件:

$ 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*

我知道您希望在目录中的文件列表中找到公共前缀,这很清楚。但我不明白的是“假设我想找到字符串的起始索引”,嗨,这,是,vijay,kumar“,它肯定会出现在每个文件中。“部分,你能解释得更好吗?例如,我给了字符串”,嗨,这,是,vijay,kumar”这在每个文件的内容中都是肯定的,而且只有一次,我想知道每个文件中提到的字符串的起始索引。我知道你想在目录中的文件列表中找到通用前缀,这很清楚。但我不明白的是“假设我想找到字符串的起始索引”,嗨,这,是,vijay,kumar“,它肯定会出现在每个文件中。“部分,你能解释得更好吗?例如,我给了字符串”,嗨,这,是,vijay,kumar”这在每个文件的内容中都会出现,而且只会出现一次,我想知道每个文件中提到的字符串的起始索引。嗨,John,这就是我在putty中运行您建议的命令时显示的内容。201606231442_6372.214:0:,CLT,************************,ES,201606231442_6373.214:0:,CLT,***************************,ES,我想在文件名后突出显示“0”尝试:
grep-ob',hi hi,这是,vijay,kumar的文件*;sed“s/:[0-9]:/$(tputsmso)和(tputso)”,这是John建议您在运行该命令时使用的方式。201606231442_6372.214:0:,CLT,*********************,ES,201606231442_6373.214:0:,CLT,************************,ES,我想在文件名后突出显示“0”尝试:
grep-ob',嗨,这是,vijay,kumar的文件*;sed“s/:[0-9]:/$(tput-smso)和(tput-so)