How can I extract a substring using regular expressions, given only the index?
2 answers
I think you need position 11 to get the match you want. Here's an example:
$ cat input.txt
This is an example00001. and so on.
$ sed -r 's|(.{10})(.{5})(.*)|\2|' input.txt
exam
$ sed -r 's|(.{11})(.{5})(.*)|\2|' input.txt
examp
What it is:
-r extended regular expressions (only on gnu sed)
s for substitution
| for separator
(.{11}) for the first group of any 11 characters (you might want 10)
(.{5}) for the second group of any 5 characters
(.*) for any other character, not really needed though
\2 for replacing with the second group
You can use the ^ and $ characters in your regex for the beginning and end of the string.
+1
source to share