similar to an older question "add header to columns from list text file"

Viewed 71

I am trying to add some headers to a txt file. Actually i have found a script already but I want to edit a part of it. Script (you can also find it here if you like: https://ucdavis-bioinformatics-training.github.io/2017-June-RNA-Seq-Workshop/thursday/counts.html):

for x in 03-alignment/*/*ReadsPerGene.out.tab; do \
    s=`basename $x | cut -f1 -d_`
    echo $s
done | paste -s > header.txt

The thing is that I want to "paste -s" but not starting from the first column but from the second. I thought done | awk $2 | paste -s | header.txt could help but is doesnt.

Any ideas how to add the headers starting directly from the 2nd column, please?

So using the script above I take this output.

L004_AQAU-19 L004_AQAU-20 L004_AQAU-21 etc. 
ALOMY0G001 0 0 
ALOMY0G002 0 10 
ALOMY0G003 20 15 
ALOMY0G004 4 5 etc. 

But i want to take the

        L004_AQAU-19  L004_AQAU-20  etc.
ALOMY0G001  0   0   
ALOMY0G002  0   10
ALOMY0G003  20  15
ALOMY0G004  4   5
etc.
1 Answers

You can make use of cut to get a range from a tab delimited line of words.

... paste -s | cut -f2- > header.txt

If you want to skip the first column, (don't have a header on the first column, just introduce an empty tab at the start.

... paste -s | sed 's/^/\t/' > header.txt

Here I use sed to replace something. You can replace like s/A/B/g where it replaces A with B. (The g indicates if this has to be done more than once per line.) These "patterns" are regular expressions (regex). For regex ^ is the beginning of the line. So here, I replace ^ (the beginning of the line) with \t (a tab). (paste uses tab by default, use paste -d, -s for example to use a comma.)

Related