I digged many threads, but neither of them adresses this question as it stands.
I am interested in addind string chr to the begining of column in each line. File is tab delimited, looks sth 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
Can be in bash oneliner
many thanks for any help, cheers, Irek
The sed command can be used to add any character of your choosing to the beginning of each line. This is the same whether you are adding a character to each line of a text file or standard input.
Insert. Insert text to the left of the cursor by typing i from command mode. Type I to insert text at the beginning of a line.
begin--fr.a2dfp.net--end begin--m.fr.a2dfp.net--end begin--mfr.a2dfp.net--end begin--ad.a8.net begin--asy.a8ww.net--end begin--abcstats.com ... The string is added only in certain lines and not in others.
Ctrl - x - Ctrl - x lets you go to the first character of the line for any readline-compatible environment like Bash, python and mysql.
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
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
sed
one-liner:
sed 's/\<[0-9]\>/chr&/' < input > output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With