输入:
$ echo -e 'AAAART5530408XXXX\nAAAAZE6530408XXXX\nAAAART12345678XXXX'
AAAART5530408XXXX
AAAAZE6530408XXXX
AAAART12345678XXXX
$
输出:
$ echo -e 'AAAART5530408XXXX\nAAAAZE6530408XXXX\nAAAART12345678XXXX' | sed -e 's/\(AAAA[A-Z]\{2\}[0-9]\{7\}\)XXXX/\n\1/g'
AAAART5530408
AAAAZE6530408
AAAART12345678XXXX
$
我怎样才能扩展:
sed -e 's/\(AAAA[A-Z]\{2\}[0-9]\{7\}\)XXXX/\n\1/g'
sed oneliner,以便它可以选择接受
sed -e 's/\(AAAA[A-Z]\{2\}[0-9]\{8\}\)XXXX/\n\1/g'
也是8个数? (不只是7)只用1个sed可能吗?
答案1
该构造是for “至少匹配和最多匹配\{7\}
”的简单情况,在您的情况下它将是:\{m,n\}
m
n
sed -e 's/\(AAAA[A-Z]\{2\}[0-9]\{7,8\}\)XXXX/\n\1/g'
也许一个简单的:
sed -s 's/XXXX//g'
你的情况就足够了吗?