Reputation: 439
I am interested in adding a string chr
to the beginning of column in each line.
The file is tab delimited and looks something like:
re1 1 AGT
re2 1 AGT
re3 2 ACGTCA
re12 3 ACGTACT
what I need is:
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT
The solution can be a bash one-liner
Upvotes: 9
Views: 15702
Reputation: 38247
Awk one-liner do?
$ awk -v OFS=$'\t' '{ $2="chr" $2; print}' so.txt
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT
Upvotes: 4
Reputation: 289495
What about this?
$ awk '$2="chr"$2' file
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT
With $2="chr"$2
we add chr
to the 2nd field. Then we do not need any other command to get the desired output, as the default behaviour of awk is print $0
.
To make sure the OFS (output field separator) is a tab, you can do the following:
$ awk 'BEGIN{OFS="\t"}$2="chr"$2' file
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT
Upvotes: 16