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?
>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