Replace first 0 by 1 in specific column
by karamunix from LinuxQuestions.org on (#5EEF6)
Hi Everyone,
I'm trying to replace in same file only the first 0 found in the fourth column by 1.
And to keep remaining 0 under the fourth column as is without any change.
To mention that columns are separated by a comma.
0,0,0,1
0,0,0,4
0,0,0,0
0,0,0,0
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2
Desired output should be:
-------------------------
0,0,0,1
0,0,0,4
0,0,0,1 --> Here the 0 in the third row for the fourth column was replaced by 1.
0,0,0,0 --> Here the 0 should not be changed.
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2
If we run the script another time:
----------------------------------
Desired output should be:
-------------------------
0,0,0,1
0,0,0,4
0,0,0,1
0,0,0,1 --> Here the 0 was replaced by 1 this time and so on.
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2
Any help would be very appreciated.
Thanks in advance.


I'm trying to replace in same file only the first 0 found in the fourth column by 1.
And to keep remaining 0 under the fourth column as is without any change.
To mention that columns are separated by a comma.
0,0,0,1
0,0,0,4
0,0,0,0
0,0,0,0
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2
Desired output should be:
-------------------------
0,0,0,1
0,0,0,4
0,0,0,1 --> Here the 0 in the third row for the fourth column was replaced by 1.
0,0,0,0 --> Here the 0 should not be changed.
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2
If we run the script another time:
----------------------------------
Desired output should be:
-------------------------
0,0,0,1
0,0,0,4
0,0,0,1
0,0,0,1 --> Here the 0 was replaced by 1 this time and so on.
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2
Any help would be very appreciated.
Thanks in advance.