I have a file look like:login1.stampede(73)$ cat mc_input 800  ! n_pass300  ! n_equil_pass6 6 6 ! simulation cell dimension in x, y, and z700   ! Tinit100   ! Tmin700   ! Tmax-10   ! TincLi -6 Fe 0   ! mu initLi -6  Fe 0  ! mu minLi 6    Fe 0  ! mu maxLi 0.3  Fe 0  ! mu inc  4  ! number of increments between structure output (0 indicates no output)  1  ! calculate average correlations yes(1) or no (0)  0  ! temp_chem = 0 if temperature runs 1 if chem runsnow I would like to target the line Li -6 Fe 0   ! mu initand replace -6 with another mu_init=-3, how could I achieve it with sed? thank you. Therefore the result should be :Li -3 Fe 0   ! mu initand Fe 0 can be all kinds of other element-number combination and we will keep it fixed, for example H 2, Ge 3.2what i currently have is: sed -i 's,^\(Li \).*! mu init,\1$mu_init'  Fe 0   ! mu init',' mc_inputbut it does not work....
replace inside a file with some specific structure
sed
New tested version after your comment about -3.333:sed -i s/^\(Li \)[0-9-][.0-9-]*\(.*mu init\)/\1${mu_init}\2/ mc_input - - - -For reference the first tested version:sed -i s/^\(Li \)[0-9-][0-9-]*\(.*mu init\)/\1${mu_init}\2/ mc_inputSome , in your version have been replaced with /.See also other modifications: double quotes, variable, etc. 