Question: Is there a way to edit a matched pattern and then replace another pattern with the edited pattern?

Input:

```
a11.t
some text here
a06.t
some text here

```

Output:

```
a11.t 11
some text here
a06.t 06
some text here

```

The above example shows the first two digits (matched by first pattern) extracted and placed at the end of the line (second pattern).

In a programming language, I would load the file into a data structure, edit, replace, and write to a new file. But is there a one-line equivalent?

Trial:

```
sed 's/\(a[0-9][0-9].*\)/& \1/I' stack.fa | sed -e 's#a##g2' -e 's#\.\w##g2'

```

Trial output:

```
a11.t 11
some text here
a06.t 06
some text here

```

Obviously the trial works, but is there a more robust way? Further, is there another text processing language this could done in more easily?

Answer: 
The simplest way is via the following:

```
$ perl -lne '$,=$"; print $_, /a(\d+)/' file
# or this 
$ perl -lpe 's/a(\d+).*\K/ $1/' file

```

```
$ awk '
    match($1, /^a[[:digit:]]+/) &&
    gsub(/$/, FS substr($1, RSTART+1, RLENGTH-1)) ||
  1' file

```

Note:it is safe to use `substr` within `gsub`'s replacement portion since we already made sure it is pure digital.
command

```
sed 's/\([a-z]\{1,\}\)\([0-9]\{2\}\)\(\..*\)/& \2/g' file

```

output

```
a11.t 11
some text here
a06.t 06
some text here

```

Python

```
#!/usr/bin/python
import re
m=re.compile(r'[0-9]')
k=open('g','r')
for i in k:
    if re.search(m,i):
        print i.strip(),i.split(".")[0][1:]
    else:
        print i.strip()

```

output

```
a11.t 11
some text here
a06.t 06
some text here

```