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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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 Smilie.


file
Code:
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
Code:
chr8	100493899	100493909	VPS13B
chr15	91543130	91543131	VPS33B

desired
Code:
chr8	100493900	100493909	VPS13B
chr15	91543130	91543131	VPS33B

awk
Code:
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

# 2  
Hi, try so
Code:
awk -F "[, -]" '
!/^VPS/         {next}
($0 ~ "-")      {print $4, $5, $6, $1; next}
                {print $4, ($5-1), $5, $1}
' OFS="\t" file

This User Gave Thanks to nezabudka For This Post:
# 3  
Hello cmccabe,

Could you please try following.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
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 Smilie.

Code:
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
Code:
chr8	100493900	100493909	VPS13B
chr15	91543130	91543131	SPS33B

The same results from your code @RavinderSingh13 .... thank you Smilie.

Last edited by cmccabe; 1 Week Ago at 10:08 AM.. Reason: added details
This User Gave Thanks to cmccabe For This Post:
# 6  
Like so?

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

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Add new column based in condition
jiam912
At begining of column 2 the same block (2000) have 3 lines, in the next block (2336) it have 9 lines and for block (2524) 3 lines. ...... Shell Programming and Scripting
0
Shell Programming and Scripting
Move input file based on condition
AngelMady
Hello, I have File1 in a directory A, a File2 in a directory B. If the File2 is not empty Then I have to move File1 from directory A to a directory archive Else no action. Is it possible to do this from one command line? Thank you in advance for your answers. Madi... Shell Programming and Scripting
2
Shell Programming and Scripting
Change in Input feed based on condition file
varunrbs
Sorry Guys for not being able to explain in one of my earlier post. I am now putting my requirement with the input file and desired output file. In the below input file Transaction code is at position 31:40. Business code is from position 318:321 TSCM00000005837 ...... Shell Programming and Scripting
7
Shell Programming and Scripting
Copy input file based on condition
charan0703
Hi, I am new to unix shell programming. I want to write a shell script for a functionality existing in mainframe system. I have one file as below as input 123456 &__987 &12yuq abcdef _ referes to blank condition:whenever the input file is having &__ ,it should be replaced...... Shell Programming and Scripting
4
Shell Programming and Scripting
Awk to print on condition in input file
pinnacle
I need only those records which has $2 equal to "DEF" independent of case (i.e upper or lower) nawk -F"," '$2 ~ //{print $0}' file This returns 3rd record also which i dont want I tried this but this doesnt work as expected. nawk -F"," '$2 == ""{print $0}' file i dont...... Shell Programming and Scripting
3
Shell Programming and Scripting