First, I would suggest using POSIX extended regexes with sed -r
here to simplify the pattern.
POSIX regexes or even POSIX extended regexes (with -r
) don't supprt \d
or \s
for character classes as for example Perl. I would use the following pattern:
sed -r 's/(a[0-9]{6}) +/\1\n/' old > new
Note that I'm capturing everything before the space into a capturing group and use this group \1
in the replacement pattern.
However, there are indeed named character classes available in POSIX regexes but they are longer to type than in Perl. If you want to use named character classes, you can use:
sed -r 's/(a[[:digit:]]{6})[[:space:]]+/\1\n/' old > new
in this case. If you want to know more about the available character classes in POSIX regexes check the manpages of grep
and/or egrep
.