Grep - 获取每次出现的行中的字符位置
Grep - Getting the character position in the line of each occurrence
根据手册,选项-b可以给出给定出现的字节偏移量,但它似乎是从解析内容的开头开始。
我需要检索grep返回的每个匹配内容的位置。我用了这条线,但它很难看:
grep '<REGEXP>' | while read -r line ; do echo $line | grep -bo '<REGEXP>' ; done
如何以更优雅的方式完成它,更有效地使用 GNU 实用程序?
示例:
$ echo "abcdefg abcdefg" > test.txt
$ grep 'efg' | while read -r line ; do echo $line | grep -bo 'efg' ; done < test.txt
4:efg
12:efg
(确实这个命令行没有输出行号,但是添加起来也不难)
Perl 不是 GNU 实用程序,但可以很好地解决您的问题:
perl -nle 'print "$.:$-[0]" while /efg/g'
在任何 UNIX 机器上的任何 shell 中使用任何 awk(GNU 或其他):
$ awk -v re='efg' -v OFS=':' '{
end = 0
while( match(substr([=10=],end+1),re) ) {
print NR, end+=RSTART, substr([=10=],end,RLENGTH)
end+=RLENGTH-1
}
}' test.txt
1:5:efg
1:13:efg
awk 中的所有字符串、字段、数组索引都从 1 开始,而不是零,因此输出看起来不像你的,因为 awk 你的输入字符串是:
123456789012345
abcdefg abcdefg
而不是:
012345678901234
abcdefg abcdefg
如果您更喜欢 0 索引字符串,请随意将上面的代码更改为 end+=RSTART-1
和 end+=RLENGTH
。
根据手册,选项-b可以给出给定出现的字节偏移量,但它似乎是从解析内容的开头开始。
我需要检索grep返回的每个匹配内容的位置。我用了这条线,但它很难看:
grep '<REGEXP>' | while read -r line ; do echo $line | grep -bo '<REGEXP>' ; done
如何以更优雅的方式完成它,更有效地使用 GNU 实用程序?
示例:
$ echo "abcdefg abcdefg" > test.txt
$ grep 'efg' | while read -r line ; do echo $line | grep -bo 'efg' ; done < test.txt
4:efg
12:efg
(确实这个命令行没有输出行号,但是添加起来也不难)
Perl 不是 GNU 实用程序,但可以很好地解决您的问题:
perl -nle 'print "$.:$-[0]" while /efg/g'
在任何 UNIX 机器上的任何 shell 中使用任何 awk(GNU 或其他):
$ awk -v re='efg' -v OFS=':' '{
end = 0
while( match(substr([=10=],end+1),re) ) {
print NR, end+=RSTART, substr([=10=],end,RLENGTH)
end+=RLENGTH-1
}
}' test.txt
1:5:efg
1:13:efg
awk 中的所有字符串、字段、数组索引都从 1 开始,而不是零,因此输出看起来不像你的,因为 awk 你的输入字符串是:
123456789012345
abcdefg abcdefg
而不是:
012345678901234
abcdefg abcdefg
如果您更喜欢 0 索引字符串,请随意将上面的代码更改为 end+=RSTART-1
和 end+=RLENGTH
。