Hi there,
how to append the header line (chromosome or scaffold) to the end of each annotation.
Input:
@Chr1
54584 54773 100 1.9 98
55019 55105 22 3.9 22
@Chr2
2226 2261 18 1.9 19
4211 4259 26 1.9 25
4426 4468 22 2 22
@utg7294
919 1455 36 15 36
919 1455 71 7.5 72
4872 4913 22 1.9 22
Expected output
54584 54773 100 1.9 98 @Chr1
55019 55105 22 3.9 22 @Chr1
2226 2261 18 1.9 19 @Chr2
4211 4259 26 1.9 25 @Chr2
4426 4468 22 2 22 @Chr2
919 1455 36 15 36 @utg7294
919 1455 71 7.5 72 @utg7294
4872 4913 22 1.9 22 @utg7294
It must be simple as search line has '@', if so append that header to end until it finds another '@'. But I couldn't find an exact script.
Thanks for your help. Sam
2 answers
DELIMITER="\t"
awk NF your.file | \
while read p; do
if [[ $p == @* ]]; then
APPEND=$p
continue
fi
if [[ $p != @* ]]; then
echo -e ${p}${DELIMITER}${APPEND}
fi
done < /dev/stdin
Change DELIMITER in line 1 according to the delimiter present in your file. Here is is tab.
Also, for the future, please use the code option (101010) in the formatting bar to indicate code and example data.
Perl:
#!/usr/bin/perl
use strict;
use warnings;
my $head = '';
while (<>) {
chomp;
if (/^(@.+)/) { $head = $1; }
else { print "$_\t$head\n"; }
}
Run as perl addheader.pl < INPUT > OUPUT
Log in to answer this question.