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: 
Although it has fallen out of fashion, few languages can match perl for text processing. For instance:

1. Assume only one set of numbers, copy to the end of the line:

```
 $ perl -pe 's/.*?a(\d+).*/$& $1/' file
 a11.t 11
 some text here
 a06.t 06
 some text here

```
2. Multiple sets of numbers, add both to the end

```
 $ cat file
 a11.t
 some text here
 a06.t
 some text here
 a11.t a54.g

 $ perl -pe '@nums=(/a(\d+)/g); s/$/ @nums/' file
 a11.t 11
 some text here 
 a06.t 06
 some text here 
 a11.t a54.g 11 54

```
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

```