Here's a sample 'test.txt'
abcdX1yad45das
abcdX2fad45das
abcdX3had45das
abcdX4wad45das
abcdX5mad45das
sample desired output:
X1yad
X2fad
X3had
X4wad
X5mad
I could get it working in vim with:
:% s/\v.*(X\d)(.*)45.*/\1\2/
and worked in perl as well with:
open(my $file, "<", "test.txt");
while(<$file>)
{
s/.*(X\d)(.*)45.*/$1$2/;
print $_;
}
my eventual regular expression needs two groupings, it wasn't required for this example output
am not able to get it work with sed:
sed -r 's/.*(X\d)(.*)45.*/\1\2/' test.txt
abcdX1yad45das
abcdX2fad45das
abcdX3had45das
abcdX4wad45das
abcdX5mad45das
just to check sed is working,
sed -r 's/(a)/#\1#/' test.txt
#a#bcdX1yad45das
#a#bcdX2fad45das
#a#bcdX3had45das
#a#bcdX4wad45das
#a#bcdX5mad45das
what am I doing wrong in sed?
[[:digit:]]
&[0-9]
. You might wish to stick on with one format – sjsam Apr 27 '16 at 06:57\d
and[[:digit:]]
are the same, but[0-9]
is different in (e.g.) Unicode where there are digit codepoints that are not within[0-9]
. – abligh Apr 27 '16 at 10:53