Question: my file:

```
chr2    10145622        10170989        11
chr7    15682126        15682183        28
chr18   48064121        48064222        10,7
chr23   20637149        20637247        824,86
chr25   2063714         2072977         824,80,62,2
chr16   48064100        48064272        10,9

```

desired output:

```
chr2    10145622        10170989        11
chr7    15682126        15682183        28
chr18   48064121        48064222        17
chr23   20637149        20637247        910
chr25   2063714 2072977 968
chr16   48064100        48064272        19

```

i tried:

```
cat test.bed | sed 's/\,/\t/g' | awk '{OFS="\t"; print $1,$2,$3,$4+NF}'

```

but its also sum blank columns which is not correct. Indeed its true, as i have replaced "," into tab.

work well if we know up to the last columns

```
cat test.bed | sed 's/\,/\t/g' | awk '{OFS="\t"; print $1,$2,$3,$4+$5+$6+$7}'

```

However, I used `NF` meaning column 4 from to last column, suppose we don't know in file, how many pattern and their numbers we have.

Answer: 
You can split the last field and sum its parts:

```
$ awk '{n=split($NF, a, ","); for (i=1;i<=n;i++) s+=a[i]; $NF=s; s=0}1' file
chr2 10145622 10170989 11
chr7 15682126 15682183 28
chr18 48064121 48064222 17
chr23 20637149 20637247 910
chr25 2063714 2072977 968
chr16 48064100 48064272 19

```

To handle input/output field separators, you can say `BEGIN {FS=OFS="\t"}`, for example.

### Explanation

* `n=split($NF, a, ",")` split the last field based on the comma separator. This way, you have an array `a` containing as many values as parts were sliced. `split()` returns the amount of "pieces", so we store it.
* `for (i=1;i<=n;i++) s+=a[i]; $NF=s; s=0` loop through the list of items in `a` and sum them in the variable `s`. (Note: `for (i in a)` would also work for this case, when the order doesn't matter). Then, set the last field to this value and reset the counter.
* `1` as it evaluates to True, it performs the default `awk` action: print current line.
`awk` has a `split` function, that splits a string based on a RE and puts the parts into an array variable, you can `split` the last field of each line on a comma, and then sum the elements of the array

```
NF==4{split($4,arr,','); $4 = 0; for(i in arr) $4+=arr[i];}
1

```

NB1 this works IF AND ONLY IF there are no spaces between the nubers in $4  

NB2 the last `1` forces the printing of the current line