Reputation: 467
I have a .bed (.tsv) file that looks like this:
chr1 0 100000
chr1 100000 200000
chr1 200000 300000
chr1 300000 425234
I want to perform the operation -1 from only values in column 3 that end in "000", using sed or awk so that the output looks like:
chr1 0 99999
chr1 100000 199999
chr1 200000 299999
chr1 300000 425234
Embarassingly enough, the best I've come up with is:
awk {sub(/000$/,"999",$3); print $1,$2,$3}' oldfile > newfile
which simply substituites the last 3 digits for 999, rather than actually subtracting. Any help is appreciated is always!
Upvotes: 0
Views: 49
Reputation: 36390
I would use GNU AWK
for this as follows, let file.txt
content be
chr1 0 100000
chr1 100000 200000
chr1 200000 300000
chr1 300000 425234
then
awk 'BEGIN{OFS="\t"}($3%1000==0){$3-=1}{print}' file.txt
output
chr1 0 99999
chr1 100000 199999
chr1 200000 299999
chr1 300000 425234
Explanation: Use tab (\t
) as output field separator (OFS
). If remainder from diving $3
by 1000
is zero (i.e. $3
is multiply of 1000
) then subtract 1
from $3
, for each line print
.
(tested in gawk 4.2.1)
Upvotes: 0
Reputation: 189377
Awk can easily perform arithmetic, too.
awk 'BEGIN{FS=OFS="\t"} $3 ~ /000$/ {$3 -= 1}1' oldfile > newfile
This is assuming all the lines in your file always have three fields and that you want to print all the lines.
sed
has no idea about even the simplest arithmetic so it's not particularly suitable for this.
Upvotes: 5