how to convert .txt to .csv using shell script ??  

Input 

```
A B 10 C d e f g
H I 88 J k l m n
O P 3  Q r s t u

```

Expected Output - After 4 blank, don't change to ','

```
A,B,10,C,d e f g
H,I,88,J,k l m n
O,P,3,Q,r s t u

```

I was trying but can't handle "d e f g"

```
$ cat input.txt | tr -s '[:blank:]' ',' > output.txt

```
['https://Stackoverflow.com/questions/45023388', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5088324/']
The `np.nonzero` command will give you the indices of all non-zero elements. So if you just want to exclude the last column, I'd do:

```
import numpy as np
x_orig = np.array([(1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0), 
               (1, 5, 9, 10, 2, 0, 0, 0, 0, 0, 1)])
row, col = np.nonzero(x_orig[:,:-1]) # these are the indices
row, col
>> (array([0, 0, 0, 1, 1, 1, 1, 1]), array([0, 1, 2, 0, 1, 2, 3, 4]))

```

Now if you *only* want the last non-zero item you can do something like:

```
keep_max = []
for i in range(x_orig.shape[0]):
    keep_max.append([i, col[row == i][-1]])
>> keep_max # again these are the indices of the last non-zero element for each row
[[0, 2], [1, 4]] # i.e. 1st row-3rd element, 2nd row-5th element

```
Example data:

```
train_data = [1,5,9,10,2,0,0,0,0,0,1]

```

If you're looking for a one-liner:

```
max([i for i, x in enumerate(train_data[:-1]) if x != 0])

```

If you're looking for efficiency, you can start from the front or end (depending on if you're expecting more or less zeros than other values) and see when the zeros start/end.

```
for i, x in enumerate(train_data):
    if x == 0:
        i = i - 1
        break

```

Note that `i` must be decremented when the first `0` is encountered to get the index of the last nonzero element.