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

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