0

I have a table in which the header is the sample list and the first column is the gene list, and the rest is expression values for each gene in each sample. I want to add a pseudocount of 1 to all values, and I currently do it as such:

cat <(head -n 1 TPM/QuickTest_Dataset.table) \
<(tail -n +2 TPM/QuickTest_Dataset.table | awk '{print $1, $2+1, $3+1, $4+1, $5+1, $6+1, $7+1, $8+1, $9+1, $10+1, $11+1, $12+1, $13+1, $14+1, $15+1, $16+1, $17+1, $18+1, $19+1, $20+1, $21+1}' | sed 's, ,\t,g') > StringTie-TPM_Homo_sapiens_GRCh38.Exp9-PMacrophageM1.protein_coding.table

However, the amount of samples isn't always 20 (amount of columns isn't always 21), sometimes more sometimes less and I have to manually adjust it each time.

Is there a simpler way?

AdrianP.
  • 435
  • 2
  • 4
  • 14
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Mar 29 '20 at 14:59

3 Answers3

3

awk is great language. Just iterate over fields and increment them.

awk '{ for (i = 2; i <= NR; ++i) $i += 1; } 1'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

You can do this with one awk call:

awk 'BEGIN{OFS="\t"} FNR==1{print; next} {for (i=2;i<=NF;i++)$i=$i+1}1' infile > outfile
Freddy
  • 4,548
  • 1
  • 7
  • 17
0

The Google-search "awk Number of fields" let me to this post, mentioning the variable NF you might use.

The search for "awk for-loop" redirects to this URL, where the following simple awk for-loop is demonstrated (this also uses the NF variable):

awk '{ for (i = 1; i <= NF; i++) total = total+$i }; END { print total }'

This might give you a good start to solve this issue.

Dominique
  • 16,450
  • 15
  • 56
  • 112