Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Awk print all columns and the modified column

Say I have a text file test

chr1    16597656        16597657        Chr1:16924151
chr1    149015385       149015386       Chr1:144869084
chr1    148989775       148989776       Chr1:144894697

I need to modify the fourth column and get the result like this

chr1    16597656        16597657     Chr1:16924151   Chr1:16924150-16924151
chr1    149015385       149015386    Chr1:144869084  Chr1:144869083-144869084
chr1    148989775       148989776    Chr1:144894697  Chr1:144894696-144894697

I tried awk -F '[:]' '{print $1":"$2-1"-"$2}' test which doesn’t print the fourth column in my result. What am I missing here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You might print the whole line and use split on the 4th column to assemble the values for the 5th column.

awk '
{
  split($4,a,":")
  print $0 OFS a[1] ":" a[2]-1 "-" a[2]
}
' file

Output

chr1    16597656        16597657        Chr1:16924151 Chr1:16924150-16924151
chr1    149015385       149015386       Chr1:144869084 Chr1:144869083-144869084
chr1    148989775       148989776       Chr1:144894697 Chr1:144894696-144894697

If you field separator is a tab:

awk '
BEGIN {FS=OFS="\t"}
{
  split($4,a,":")
  print $0 OFS a[1] ":" a[2]-1 "-" a[2]
}
' file

Output

chr1    16597656        16597657        Chr1:16924151   Chr1:16924150-16924151
chr1    149015385       149015386       Chr1:144869084  Chr1:144869083-144869084
chr1    148989775       148989776       Chr1:144894697  Chr1:144894696-144894697
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading