Search code examples
linuxawksedex

Edit part of a line in text without losing other lines


I tried to replace the tstop parameter of the text from 120 to 80. What I got was a single line of text: tstop 80, losing the rest of the text. I used

sed -i -rne 's/(tstop)\s+\w+/\1 80/gip'

I want to change only the line tstop and keep the rest of text as it is.

Part of the text is:

[Grid]    
X1-grid    1     -6.0  24 u 6.0  
X2-grid    1     -24.   96 u 24.    
X3-grid    1     -18.0  72 u 18.0    
[Chombo Refinement]   
Levels           4     
Ref_ratio        2 2 2 2 2     
Regrid_interval  2 2 2 2     
Refine_thresh    0.3    
Tag_buffer_size  3    
Block_factor     8    
Max_grid_size    64    
Fill_ratio       0.75    
[Time]    
CFL              0.3    
CFL_max_var      1.1    
tstop            120    
first_dt         1.e-5    
[Solver]    
Solver         tvdlf

Solution

  • with GNU sed:

    sed -E 's/^(tstop +)[^ ]*/\180/' file
    

    or

    sed -E '/^tstop/s/[^ ]+$/80/' file
    

    If you want to edit your file "in place" use sed's option -i.


    See: The Stack Overflow Regular Expressions FAQ