Quote:
Originally Posted by warrentaylor
if I have .....AA9999999999999999....., I want AA9999999999999999
if I have ............................, I want nothing.
where AA9999999999999999 is 2 capital alphas followed by 16 numerics.
I use 's/.*\(AA[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\).*/\1/'
because \{16\} as a repeater doesn't work.
|
Repeaters are extended regexp functions, meaning you have use "sed -r" or "grep -E/egrep". On the plus side, this also means you don't have to escape the parentheses or brackets.
Also, it's recommended to use the posix matching classes for the standard ranges of characters. Either of the following should work:
Code:
sed -rn 's/.*(AA[[:digit:]]{16}).*/\1/p'
egrep -o 'AA[[:digit:]]{16}'
Note that there are a couple of weaknesses in the above, though they may or may not be a concern for you. First, it will match number strings of any length, but only print the first 16. Second, it will match any combination of numerals, meaning something like AA1234567890123456 will also match. I'm not sure what you'd need to do if you need to isolate only a single repeating number.