new line to tab in every second column
3
0
Entering edit mode
8.6 years ago
mbk0asis ▴ 700

Hi.

I have a Primer3 result txt file which all the primers are in one column.

0    ATTTTATTNGAAATAGTAGTGGG
0    CTCCCAAAATACTAAAATTATAA
1    TTTTAGTTATTTANGAGGTTGAG
1    CNTAATCTTAACTCACTACAACC
2    TTATAATTTTAGTATTTTGGGAG
2    CATATTAACCAAACTAATCTTAA
3    GGTTAATATGGTGAAATTTAAT
3    ACCTCAACCTCNTAAATAACTAA

As all you can guess, I want to paste every second lines (RIGHT) next to first ones (LEFT) to look like below.

0    ATTTTATTNGAAATAGTAGTGGG   TTTTAGTTATTTANGAGGTTGAG
1    TTTTAGTTATTTANGAGGTTGAG   CNTAATCTTAACTCACTACAACC
2    TTATAATTTTAGTATTTTGGGAG   CATATTAACCAAACTAATCTTAA
3    GGTTAATATGGTGAAATTTAAT    ACCTCAACCTCNTAAATAACTAA

I think using 'awk' would be easy and simple, so I was trying to run something like below. However, I can't really figure that out.

awk 'if($1 is equal or pairs) {new line to tab}'

It doesn't have be 'awk', but it would be great if I can do batch run.

If anyone have any ideas, please enlight me!

Thank you!

linux bash • 2.3k views
ADD COMMENT
4
Entering edit mode
8.6 years ago
Ming Tommy Tang ★ 4.5k

even more elegant:

cat test.txt| paste - -                               
0    ATTTTATTNGAAATAGTAGTGGG    0    CTCCCAAAATACTAAAATTATAA
1    TTTTAGTTATTTANGAGGTTGAG    1    CNTAATCTTAACTCACTACAACC
2    TTATAATTTTAGTATTTTGGGAG    2    CATATTAACCAAACTAATCTTAA
3    GGTTAATATGGTGAAATTTAAT     3    ACCTCAACCTCNTAAATAACTAA
ADD COMMENT
0
Entering edit mode

Wow! It's so simple. Thank you very much, tangming2005!

ADD REPLY
0
Entering edit mode
8.6 years ago
Ming Tommy Tang ★ 4.5k

This should do the trick. find it here http://serverfault.com/questions/375096/bash-sed-awk-etc-remove-every-other-newline

cat test.txt| awk 'ORS=NR%2?"\t":"\n"'          

0    ATTTTATTNGAAATAGTAGTGGG    0    CTCCCAAAATACTAAAATTATAA
1    TTTTAGTTATTTANGAGGTTGAG    1    CNTAATCTTAACTCACTACAACC
2    TTATAATTTTAGTATTTTGGGAG    2    CATATTAACCAAACTAATCTTAA
3    GGTTAATATGGTGAAATTTAAT     3    ACCTCAACCTCNTAAATAACTAA
ADD COMMENT
0
Entering edit mode
8.6 years ago
5heikki 11k

Output delimiter is space though:

xargs -L2 < file
0 ATTTTATTNGAAATAGTAGTGGG 0 CTCCCAAAATACTAAAATTATAA
1 TTTTAGTTATTTANGAGGTTGAG 1 CNTAATCTTAACTCACTACAACC
2 TTATAATTTTAGTATTTTGGGAG 2 CATATTAACCAAACTAATCTTAA
3 GGTTAATATGGTGAAATTTAAT 3 ACCTCAACCTCNTAAATAACTAA

We can fix it with tr:

xargs -L2 < file | tr " " "\t"
0   ATTTTATTNGAAATAGTAGTGGG 0   CTCCCAAAATACTAAAATTATAA
1   TTTTAGTTATTTANGAGGTTGAG 1   CNTAATCTTAACTCACTACAACC
2   TTATAATTTTAGTATTTTGGGAG 2   CATATTAACCAAACTAATCTTAA
3   GGTTAATATGGTGAAATTTAAT  3   ACCTCAACCTCNTAAATAACTAA
ADD COMMENT

Login before adding your answer.

Traffic: 2457 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6