Awk print all columns and the modified column

Viewed 110

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?

6 Answers

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

Here is an alternative gnu-awk 1-liner solution:

awk '{print $0 "\t" gensub(/:/, "&" (gensub(/[^:]+:/, "", 1, $NF)-1) "-", 1, $NF)}' file

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

With your shown samples please try following awk code. Written and tested in GNU awk.

awk '
BEGIN{OFS="\t"}
match($0,/.*[[:space:]]([^:]+):([0-9]+)$/,arr){
  print $0,arr[1]":"arr[2]-1"-"arr[2]
}
' Input_file
# awk -F: 'BEGIN{OFS="\t"}{a="Chr1:"($2-1)"-"$2;print $0,a}' file 
chr1    16597656        16597657        Chr1:16924151   Chr1:16924150-16924151
chr1    149015385       149015386       Chr1:144869084  Chr1:144869083-144869084
chr1    148989775       148989776       Chr1:144894697  Chr1:144894696-144894697
$ xargs -n 4 bash -c 'echo -e "$1\t$2\t$3\t$4\t${4/:*/}:$((${4/*:/}-1))-${4/*:/}" ' argv0 <file
chr1    16597656        16597657        Chr1:16924151   Chr1:16924150-16924151
chr1    149015385       149015386       Chr1:144869084  Chr1:144869083-144869084
chr1    148989775       148989776       Chr1:144894697  Chr1:144894696-144894697

mawk/gawk ::

awk 'sub("[^:]+$","&\t&=\t-&")^--$5+sub("\t+[-]","-")+sub("[^\t]+$","Chr1:&")'

=

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