Irek
Irek

Reputation: 439

add string in each line at the begining of column

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

Answers (3)

mohit
mohit

Reputation: 6044

sed one-liner:

sed 's/\<[0-9]\>/chr&/' < input > output

Upvotes: 1

MattH
MattH

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

fedorqui
fedorqui

Reputation: 289495

What about this?

$ awk '$2="chr"$2' file
re1 chr1 AGT
re2 chr1 AGT
re3 chr2 ACGTCA
re12 chr3 ACGTACT

Explanation

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

Related Questions