-1

I have a table containing several thousands line like this

A   GO:0008150,GO:0050789,GO:0050794,GO:0051726,GO:0065007
B   GO:0008150,GO:0050789,GO:0050794,GO:0051726,GO:0065007

I want to parse my table in the following format.

A   GO:0008150
A   GO:0050789
A   GO:0050794
A   GO:0051726
A   GO:0065007
B   GO:0008150
B GO:0050789
B GO:0050794
B GO:0051726
C GO:0065007

Any help will be greatly appreciated. Thanks

pali
  • 195
  • 2
  • 14

3 Answers3

1

Easy with awk: just split() the second column and loop through the slices:

$ awk '{n=split($2, a, ","); for (i=1;i<=n;i++) print $1,a[i]}' file
A GO:0008150
A GO:0050789
A GO:0050794
A GO:0051726
A GO:0065007
B GO:0008150
B GO:0050789
B GO:0050794
B GO:0051726
B GO:0065007
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1

awk without loops, requires multi-char RS.

$ awk -v RS=",|\n" 'NF==2{t=$1;$1=$2} {print t,$1}' file
karakfa
  • 66,216
  • 7
  • 41
  • 56
0

You can use Python with the re module.

import re
text = '''A   GO:0008150,GO:0050789,GO:0050794,GO:0051726,GO:0065007
B   GO:0008150,GO:0050789,GO:0050794,GO:0051726,GO:0065007'''
pattern = {
'A': re.compile('A\s+(GO.*)\n'),
'B': re.compile('B\s+(GO.*)\n*')
}
A = 'A  ' + '\nA  '.join(pattern['A'].findall(text)[0].split(','))
B = 'B  ' + '\nB  '.join(pattern['B'].findall(text)[0].split(','))
print A
print B

Output:

A  GO:0008150
A  GO:0050789
A  GO:0050794
A  GO:0051726
A  GO:0065007
B  GO:0008150
B  GO:0050789
B  GO:0050794
B  GO:0051726
B  GO:0065007
Borodin
  • 126,100
  • 9
  • 70
  • 144
Jianli Cheng
  • 371
  • 1
  • 8
  • 17