Search code examples
pythonbashperlswapfasta

Swap lines in a script by iteration over lines


Here is my file:

>ref
AAAAAAA
>seq1
BBBBBBB
>ref
AAAAAAA
>seq2
CCCCCCC
>ref
AAAAAAA
>seq3
DDDDDDD
...

Here is what I'd like to get:

>seq1
AAAAAAA
>ref
BBBBBBB
>seq2
AAAAAAA
>ref
CCCCCCC
>seq3
AAAAAAA
>ref
DDDDDDD
...

So, swap line 1 with line 3, line 5 with line 7, line 9 with line 11, etc. Any suggestion on how I could do it (in bash, perl or python) will be much appreciated! :)


Solution

  • Using perl from command line,

    perl -ne 'push @r, $_; print(@r[2,1,0,3]), @r=() if @r==4 or eof' file