0

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
Benjamin W.
  • 33,075
  • 16
  • 78
  • 86
Spirtoylis
  • 15
  • 4

2 Answers2

0

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

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117
  • This is exactly what I want. I just have 1 question what does means the */\180/ so i understand better how sed works. – Spirtoylis Mar 04 '17 at 09:59
  • The parentheses `()` mark a capturing group and `\1` means use here the value from first capturing group. – Cyrus Mar 04 '17 at 10:04
0

The n flag in -rne suppresses the normal output of the sed command. Only lines matching your pattern will be output with your p command. Try this:

sed -i -re 's/(tstop)\s+\w+/\1 80/gi'

A more portable version using BRE(Basic Regular Expresssions) could be:

sed -i -e 's/\(tstop\)\(  *\)[[:alnum:]]*/\1\280/' file

Note that spaces after tstop are also captured here, to preserve the file format. Also i and g modifiers seem to be useless in your case.

SLePort
  • 14,405
  • 3
  • 28
  • 39