Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

INPUT:

 $ echo -e 'AAAART5530408XXXX\nAAAAZE6530408XXXX\nAAAART12345678XXXX'
AAAART5530408XXXX
AAAAZE6530408XXXX
AAAART12345678XXXX
$

OUTPUT:

 $ echo -e 'AAAART5530408XXXX\nAAAAZE6530408XXXX\nAAAART12345678XXXX' | sed -e 's/\(AAAA[A-Z]\{2\}[0-9]\{7\}\)XXXX/\n\1/g'

AAAART5530408

AAAAZE6530408
AAAART12345678XXXX
$   

How can I extend the:

sed -e 's/\(AAAA[A-Z]\{2\}[0-9]\{7\}\)XXXX/\n\1/g'

sed oneliner, so that it would optionally accept

sed -e 's/\(AAAA[A-Z]\{2\}[0-9]\{8\}\)XXXX/\n\1/g'

8 numbers too? (not just 7) Is it possible with only 1 sed?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The \{7\} construct is a simple case of the \{m,n\} for "match at least m and at most n, in your case it'll be:

sed -e 's/\(AAAA[A-Z]\{2\}[0-9]\{7,8\}\)XXXX/\n\1/g'

Perhaps a simple:

sed -s 's/XXXX//g'

is enough in your case?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.