add string in each line at the begining of column

awk, bash, string

Solution

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

Problem

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

Original source