brucezepplin
brucezepplin

Reputation: 9752

split file into single lines via delimiter

Hi I have the following file:

>101
ADFGLALAL
GHJGKGL
>102
ASKDDJKJS
KAKAKKKPP
>103
AKNCPFIGJ
SKSK

etc etc;

and I need it in the following format:

>101
ADFGLALALGHJGKGL
>102
ASKDDJKJSKAKAKKKPP
>103
AKNCPFIGJSKSK

how can I do this? perhaps a perl one liner?

Thanks very much!

Upvotes: 1

Views: 142

Answers (2)

perreal
perreal

Reputation: 97938

perl -npe 'chomp if ($.!=1 && !s/^>/\n>/)' input

Remove the newline at the end (chomp) if there is no > at the beginning (!s/^>/\n>/ is false). Also, add a newline at the beginning of the line if this is not the first line ($.!=1) and there is a > at the beginning of the line (s/^>/\n>/).

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185025

perl -lne '
    if (/^>/) {print}
    else{
        if ($count) {
            print $string . $_;
            $count = 0;
        } else {
            $string = $_;
            $count++;
        }
    }
' file.txt

Upvotes: 0

Related Questions