awk script

Hi,

I have a file which has regions in numbers like:

chr1    6845635    6880240

and I want an awk script which extracts only 1000 numbers from the end of the 2nd column. The output should then look like this:

chr1    6845635    6846635

Kindly help.
warm regards.

Hi genebuster,

Welcome to forum, please use code tags while posting codes/commands in posts, you can Preview Post option before posting it.
Following may help you in same.

awk -vvar=1000 '{$2==6845635} (++i) {if(i<=var){print $0}}'  Input_file

Thanks,
R. Singh

My File has different entries in every line.

chr1    6845635    6880240    
chr1    7151431    7309550    
chr1    57756838    58115263    
chr1    85824530    85961848    
chr1    172100428    172222712

and I want that the script should work for each line adding 1000 as a number from each of the second columns. Like

chr1 6845635 6846635
chr1 7151431 7152431

and so on...
Thanks

---------- Post updated at 09:18 AM ---------- Previous update was at 09:09 AM ----------

Hi Mr Singh,
Thanks but your code is not working.

Hello genebuster,

Following may help.

awk '($3=$2+1000) 1' Input_file

Assuming like your file not having any empty lines init.

Thanks,
R. Singh

1 Like

Like this ?

awk 'NF { $3 = $2 + 1000 }1' file
1 Like

Surprisingly the Tabs have disappeared in the output file...how to put them back ?

awk 'NF { $3 = $2 + 1000 }1' OFS='\t' file

---------- Post updated at 09:29 PM ---------- Previous update was at 09:22 PM ----------

If suppose in case you want to keep source formating you can do like this, though its bit lengthy

awk 'NF { s=""; n=split($0,D,/[^[:space:]]*/); $3 = $2 + 1000; for(i=1;i<=n;i++)s = sprintf("%s%s%s",s,D,$i); $0 = s}1' file

chr1    6845635    6846635    
chr1    7151431    7152431    
chr1    57756838    57757838    
chr1    85824530    85825530    
chr1    172100428    172101428
1 Like