Add character to numeric value in column and print other columns

Hi there,

I want to add the following characters "chr" to the beginning of the first column of my data and print out the remaining columns.

sample.txt

1 10176 10177 rs367896724
1 10234 10235 rs540431307
1 10351 10352 rs555500075

data_desired.txt

chr1 10176 10177 rs367896724
chr1 10234 10235 rs540431307
chr1 10351 10352 rs555500075

I've developed the following code to do this, but it's taking a very long time to run and I'm concerned there is an issue. Does anyone see anything wrong with what I have? I'm new to shell scripting so any help would be greatly appreciated!

awk '{sub("^", "chr", $1)}; 1'; '{print $1,$2,$3,$4}' sample.txt > data_desired.txt

either:

sed 's/^/chr/' myFile

or

awk '$0="chr" $0' myFile
1 Like

Thank you!!

Your initial attempt should work as well

awk '{sub("^", "chr", $1)} 1' sample.txt

The 1 as an always-true condition does a default print action. Or you can do an explicit print

awk '{sub("^", "chr", $1); print}' sample.txt
1 Like

@ellie_story_2020, could you please try following too.

awk '{print "chr" $0}'  Input_file

Thanks ,
R. Singh

1 Like