awk to add +1 to value based on condition in input

In the awk below I am trying to add a | that will adjust $2 in the ouput by adding +1 if the original value from file that was used in $3 had a - in it. Line 3 of file is an example of this. In my current awk I just subtract one but I am not sure how to only apply this to those values without a - . In my actual data there may be hundreds of lines but always the same format. Thank you :).

file

2019 NGS-A Gene and Chromosomal Position List,,,,
Gene Symbol,HGNC ID,Transcript Number,Chromosomal Position/Interval ,Required (Exome and Genome Labs)
VPS13B,HGNC:2183,NM_017890.4,chr8 100493900-100493909,yes
VPS33B,HGNC:12712,NM_018668.4,chr15 91543131,

current

chr8	100493899	100493909	VPS13B
chr15	91543130	91543131	VPS33B

desired

chr8	100493900	100493909	VPS13B
chr15	91543130	91543131	VPS33B

awk

awk -F, 'FNR > 2 {for(i=1;i<=NF;i++){if($i~/^chr/){a=$i}} print a,$1}' file | awk -F'[ :-]' 'NF==3{$4=$3;$3=$2} {$1=$1} 1' OFS='\t' | awk -F'\t' '{print $1,$2-1,$3,$4}' OFS="\t" out

Hi, try so

awk -F "[, -]" '
!/^VPS/         {next}
($0 ~ "-")      {print $4, $5, $6, $1; next}
                {print $4, ($5-1), $5, $1}
' OFS="\t" file
1 Like

Hello cmccabe,

Could you please try following.

awk -F, '/VPS/{num=split($4,array,"[ -]");if(num==3){print array[1],array[2]+1,array[3],$1};if(num==2){print array[1],array[2]-1,array[2],$1}}'   Input_file | column -t

Thanks,
R. Singh

1 Like

Close, but the ID -1 Transcript Gene doesn't need to print and even when I add FNR>2 it does. I added [A-Z] as the VPS may be different but it will always be capital letters. Thank you :).

awk -F "[, -]" '
!/^[A-Z]/         {next}
($0 ~ "-")      {print $4, $5, $6, $1; next}
                {print $4, ($5-1), $5, $1}
' OFS="\t" in

ID	-1	Transcript	Gene
chr8	100493900	100493909	VPS13B
chr15	91543130	91543131	SPS33B

desired

chr8	100493900	100493909	VPS13B
chr15	91543130	91543131	SPS33B

The same results from your code @RavinderSingh13 .... thank you :).

1 Like

may be

!/:/    {next}

or

!/^[A-Z][A-Z]/    {next}

Like so?

awk -F '[, -]' '$4~/^chr/{if(!$6) {$6=$5; $5-=1} print $4, $5, $6, $1}' OFS='\t' file
1 Like