0

I have a file with an unique column and I would like to split this column in multiple columns according to a specific string (chr). The number of Items between the first string and the second, the secon and the third, the n and the m... is not regular.

Input looks like this:

chr10:127293562-127293909
BRUNOL4(Hs/Mm)
CPEB4(Hs/Mm)
CUG-BP(Hs/Mm)
DAZAP1(Hs/Mm)
ENOX1(Hs/Mm)
FMR1(Hs/Mm)
chr11:49214073-49214804
BRUNOL4(Hs/Mm)
BRUNOL5(Hs/Mm)
CPEB2(Hs/Mm)
CPEB4(Hs/Mm)
CUG-BP(Hs/Mm)
HNRNPC(Hs/Mm)
HNRNPCL1(Hs/Mm)
HNRNPH1(Hs/Mm)
HuR(Hs/Mm)
MBNL1(Hs/Mm)
NOVA1(Hs/Mm)
chr11:49854587-49855127
A1CF(Hs/Mm)
BRUNOL4(Hs/Mm)

The output should look like this:

chr10:127293562-127293909  chr11:49214073-49214804  chr11:498547-498551
BRUNOL4(Hs/Mm)             BRUNOL4(Hs/Mm)           A1CF(Hs/Mm)
CPEB4(Hs/Mm)               BRUNOL5(Hs/Mm)           BRUNOL4(Hs/Mm)
CUG-BP(Hs/Mm)              CPEB2(Hs/Mm)
DAZAP1(Hs/Mm)              CPEB4(Hs/Mm)    
ENOX1(Hs/Mm)               CUG-BP(Hs/Mm)
FMR1(Hs/Mm)                HNRNPC(Hs/Mm)
                           HNRNPCL1(Hs/Mm)
                           HNRNPH1(Hs/Mm)
                           HuR(Hs/Mm)
                           MBNL1(Hs/Mm)
                           NOVA1(Hs/Mm)
1
  • use the second answer to your previous question on this subject.
    – meuh
    Sep 21, 2016 at 16:02

2 Answers 2

1
$ csplit -zsf file -n 1 ip.txt /^chr/ {*} ; paste file* | column -nt
chr10:127293562-127293909  chr11:49214073-49214804  chr11:49854587-49855127
BRUNOL4(Hs/Mm)             BRUNOL4(Hs/Mm)           A1CF(Hs/Mm)
CPEB4(Hs/Mm)               BRUNOL5(Hs/Mm)           BRUNOL4(Hs/Mm)
CUG-BP(Hs/Mm)              CPEB2(Hs/Mm)             
DAZAP1(Hs/Mm)              CPEB4(Hs/Mm)             
ENOX1(Hs/Mm)               CUG-BP(Hs/Mm)            
FMR1(Hs/Mm)                HNRNPC(Hs/Mm)            
                           HNRNPCL1(Hs/Mm)          
                           HNRNPH1(Hs/Mm)           
                           HuR(Hs/Mm)               
                           MBNL1(Hs/Mm)             
                           NOVA1(Hs/Mm)             
  • csplit used to split files based on a pattern
    • -z option to delete empty files (for cases like pattern matching first line itself)
    • -s suppress log output
    • -f file -n 1 output files name start with file and single digit suffix
    • ip.txt is input file, /^chr/ is pattern to work on
    • {*} as many splits as possible
  • paste is then used to concatenate the split files column wise
  • column -nt used to style the output from paste, -n prevents default behavior of column to merge adjacent delimiters and a GNU extension
2
  • could you explain please the code? Thanks in advance Sundepp Sep 22, 2016 at 19:19
  • 1
    @Tommi, added explanation...
    – Sundeep
    Sep 23, 2016 at 2:13
0

With without any pipe :

#!/usr/bin/env perl

use strict; use warnings;

my $c = -1; my $arr = [];

while (<>) {
    if (/^chr/) {$c++};
    chomp;
    push(@{ $arr->[$c] }, $_);
}

foreach my $i (0...scalar(@{ $arr->[1] }) -1) {
    printf("%-30s %s\n", $arr->[0]->[$i], $arr->[1]->[$i]);
}

OUTPUT

chr10:127293562-127293909      chr11:49214073-49214804
BRUNOL4(Hs/Mm)                 BRUNOL4(Hs/Mm)
CPEB4(Hs/Mm)                   BRUNOL5(Hs/Mm)
CUG-BP(Hs/Mm)                  CPEB2(Hs/Mm)
DAZAP1(Hs/Mm)                  CPEB4(Hs/Mm)
ENOX1(Hs/Mm)                   CUG-BP(Hs/Mm)
FMR1(Hs/Mm)                    HNRNPC(Hs/Mm)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.