Change the i-th character of the j-th line

Hi,
I have a file like

#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1 
1.03916e-03   1  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 

Is there a way to change the i-th character of the j-th line to a value that I specifiy?

In the case above I would like to change from 1 to 0 the character in the 15th column; but it can be anywhere in principle

Any help is appreciated,
Sarah

$  sed "/.\{14\}1/s/./0/15" file
#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1
1.03916e-03   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
1 Like

is there a way to specify the row number? I would like to change only one row+column

Try an adaption of anbu's code:

sed "4s/^\(.\{14\}\)1/\10/" file

Please note that anbu's regex patterns should be anchored to target exactly the one location intended.

Let's say that I want to change the character in row 3, column 15 to X

Input

#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1 
1.03916e-03   1  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1

output

#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1 
1.03916e-03   X  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1 
0.00000e+00   0  ! fix=0) free=1

Hi, in bash:

$ ROW=3 && COL=15 && sed -e "$((ROW+1))s/./X/${COL}" file
#-----------------------------------
3                ! number of parameters of the polynome
0.00000e+00   0  ! fix=0) free=1
1.03916e-03   X  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
3                ! number of parameters
2.16595e-05   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1
0.00000e+00   0  ! fix=0) free=1

Regards

1 Like