Daisy
Daisy

Reputation: 1

Backreference with sed

I am trying to rearrange headers in my fasta file. I thought I could select the first 10 characters, then the rest of the line, and then backreference the first selection to move it to the end.

AY843768_1 Genus species 12S

would then be

Genus species 12S AY843768_1

I need to use sed for a learning exercise, but I am unsure how to make the selections to switch the 10-digit ID to the end of the line for every header in my file.

sed ‘s/^\(.\{10\}\)(.*$)/\2 \1/g' file1.fasta > file2.fasta

Upvotes: 0

Views: 438

Answers (1)

Corvus
Corvus

Reputation: 11

This will do it:

sed 's/^\(.\{10\}\) \(.*\)/\2 \1/' file1.fasta > file2.fasta
  • backslashes at the second braces where missing.
  • .* already matches the rest of the line so $ isn't needed
  • g isnt needed cause multiple matches per line arent possible if expression begins with ^

Anyway, using ERE instead of BRE makes it more readable:

sed -E 's/^(.{10}) (.*)/\2 \1/' file1.fasta > file2.fasta

OT: Same (or similar) can be achieved with bash only:

while read id rest;do echo "$rest $id";done <file1.fasta >file2.fasta

Upvotes: 1

Related Questions