0

I have a tab delimited text file like this-

test.txt

chrom1  start1  end1
    2   8828280 8828281
    2   8828952 8828953
    2   115627275   115627276
    3   63945545    63945546
    3   109753479   109753480
    3   109753640   109753641
    4   31116488    31116489
    4   31116523    31116524

How can I do the following tasks in a unix shell-

  1. change the column name "chrom1" to "chr" and
  2. add "chr" in front of the each values in column "chr".

The output should look like -

 chr    start1  end1
 chr2   8828280 8828281
 chr2   8828952 8828953
 chr2   115627275   115627276
 chr3   63945545    63945546
 chr3   109753479   109753480
 chr3   109753640   109753641
 chr4   31116488    31116489
 chr4   31116523    31116524

1 Answer 1

1

You can use awk:

awk 'BEGIN{FS=OFS="\t"} {$1 = "chr" (NR==1 ? "" : $1)} 1' file

chr   start1     end1
chr2  8828280    8828281
chr2  8828952    8828953
chr2  115627275  115627276
chr3  63945545   63945546
chr3  109753479  109753480
chr3  109753640  109753641
chr4  31116488   31116489
chr4  31116523   31116524
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @anubhava. If I want to do the same two changes also on column 4 of the same file, could you suggest how to do that?
Sorry I don't understand as your question only mentions first column.
If the changes have to be made on column 4 instead of column 1.
Just change $1 to $4 as awk 'BEGIN{FS=OFS="\t"} {$4 = "chr" (NR==1 ? "" : $4)} 1' file
I think OFS="\t" is enough otherwise first column does not look alright.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.