How Can I replace specific lines in a text file

4 views (last 30 days)
I'm dealing with text files and I want to replace certain lines in the text. I do not know their location but I know the text in an above and a later line to those lines.
Example for that text
TABLE: "LINK PROPERTY DEFINITIONS 02 - LINEAR" Link="Rails Pinned" DOF=U1 Fixed=Yes Link="Rails Pinned" DOF=U2 Fixed=Yes
TABLE: "MASS SOURCE" MassSource=MSSSRC1 Elements=Yes Masses=Yes Loads=No IsDefault=Yes
TABLE: "MATERIAL PROPERTIES 04 - USER STRESS-STRAIN CURVES" Material="Timber Ties" Strain=-0.00078125 Stress=-1
I would like to replace the line/lines after TABLE: "MASS SOURCE" and before the following word TABLE: The word table repeats many times in the original text and because of that I need the following word TABLE: Is it possible?
Thank you
  2 Comments
Cedric
Cedric on 22 Sep 2017
What do you want to replace? There are plenty of tools for doing this, but we need to know exactly what you need to achieve. The best is to give a slice of this file with a few occurrences of what you need to replace, and to give an example of output after replacement.
Ahmed Rageh
Ahmed Rageh on 22 Sep 2017
Edited: Cedric on 22 Sep 2017
An Example for the Original Text file
TABLE: "SECTION DESIGNER PROPERTIES 30 - FIBER GENERAL"
SectionName=BOT-LATERALS-L6*4*1/2 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=BOT-LATERALS-L6*4*3/8 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=BOT-LATERALS-L6*4*5/8 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=F-BEAM-END-SEC-1 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=F-BEAM-END-SEC-2 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=F-BEAM-END-SEC-3 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=F-BEAM-END-SEC-4 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=F-BEAM-INTERMEDIATE-SEC-1 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=F-BEAM-INTERMEDIATE-SEC-2 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=F-BEAM-INTERMEDIATE-SEC-3 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=F-BEAM-INTERMEDIATE-SEC-4 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L0-L2 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L0-U1 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L1-U1 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L2-L3 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L2-U1 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L2-U2 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L2-U3 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L3-U2 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L3-U3 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=L4-U4 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=PORTAL-STRUT-2L6*4*1/2+PL.12*3/8 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=PORTAL-STRUT-KNEE-2L4*3.5*3/8 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=STRINGER NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=STRINGER-REPLACED NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=STR-LATERALS-L3*3*3/8 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=TOP-LATERALS-L3.5*3.5*3/8 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=TOP-STRUT-2L6*3.5*3/8 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=TOP-STRUT-KNEE-2L4*3*3/8 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=U1-U2 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
SectionName=U2-U3 NumFibersD2=3 NumFibersD3=3 CoordSys=Cartesian GridAngle=0 LumpRebar=No FiberPMM=No FiberMC=No
TABLE: "SOLID PROPERTY DEFINITIONS"
SolidProp=Solid1 Material=4000Psi MatAngleA=0 MatAngleB=0 MatAngleC=0 InComp=Yes Color=Cyan
TABLE: "VEHICLES 2 - GENERAL VEHICLES 1 - GENERAL"
VehName=Train#1-WHL1 StayInLane=No
VehName=Train#1-WHL2 StayInLane=No
TABLE: "VEHICLES 3 - GENERAL VEHICLES 2 - LOADS"
VehName=Train#1-WHL1 LoadType="Fixed Length" UnifLoad=0 AxleLoad=34.62 MinDist=1
VehName=Train#1-WHL1 LoadType="Fixed Length" UnifLoad=0 AxleLoad=32.95 MinDist=77.76
VehName=Train#1-WHL1 LoadType="Fixed Length" UnifLoad=0 AxleLoad=34.35 MinDist=77.77
VehName=Train#1-WHL1 LoadType="Fixed Length" UnifLoad=0 AxleLoad=34.15 MinDist=389.74
TABLE: "VEHICLES 4 - VEHICLE CLASSES"
VehClass=Train#1-WHL1 VehName=Train#1-WHL1 ScaleFactor=1
VehClass=Train#1-WHL2 VehName=Train#1-WHL2 ScaleFactor=1
END TABLE DATA
And I wrote this text file using Matlab which I would like to move it to upper text file
TABLE: "VEHICLES 3 - GENERAL VEHICLES 2 - LOADS"
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=34.62
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=32.95 MinDist=77.76
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=34.35 MinDist=77.77
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=34.15 MinDist=389.74
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=36.15 MinDist=77.78
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=34.39 MinDist=77.79
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=6.37 MinDist=121.87
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.38 MinDist=69.15
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.14 MinDist=409.77
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=4.77 MinDist=69.17
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.08 MinDist=82.14
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.04 MinDist=68.31
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.44 MinDist=409.91
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.00 MinDist=68.32
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.14 MinDist=82.17
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.12 MinDist=68.33
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.36 MinDist=410.03
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=4.78 MinDist=68.34
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.42 MinDist=82.19
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.76 MinDist=69.22
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.63 MinDist=410.13
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=6.13 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.05 MinDist=82.20
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.80 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.53 MinDist=411.04
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.76 MinDist=69.23
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.07 MinDist=81.34
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.56 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.33 MinDist=410.19
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.25 MinDist=69.23
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=4.99 MinDist=82.21
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.37 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.28 MinDist=410.17
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=4.99 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.97 MinDist=81.34
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.35 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.79 MinDist=410.16
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.65 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=4.62 MinDist=82.20
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=4.95 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.51 MinDist=410.15
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.99 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.57 MinDist=82.20
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.43 MinDist=69.22
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.69 MinDist=410.16
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.45 MinDist=69.23
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.49 MinDist=82.21
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.62 MinDist=68.36
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.65 MinDist=411.06
VehName="Train#1-WHL1" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.85 MinDist=69.23
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=33.28
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=34.93 MinDist=77.76
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=33.75 MinDist=77.77
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=32.75 MinDist=389.74
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=36.11 MinDist=77.78
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=32.46 MinDist=77.79
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=5.30 MinDist=121.87
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=4.75 MinDist=69.15
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=6.89 MinDist=409.77
VehName="Train#1-WHL2" LoadType="Fixed Length" UnifLoad=0 AxleLoad=6.87 MinDist=69.17
I would like to replace the three lines below the heading TABLE: "VEHICLES 3 - GENERAL VEHICLES 2 - LOADS" with the number of lines in the new text file

Sign in to comment.

Accepted Answer

Cedric
Cedric on 22 Sep 2017
Edited: Cedric on 22 Sep 2017
Here is an example, but we can be much more specific.
% - Read source.
content = fileread( 'myData.txt' ) ;
% - Update.
content = regexprep( content, 'TABLE:\s+"MASS SOURCE".*?(?=[\r\n]+TABLE)', 'Hello World' ) ;
% - Export update.
fId = fopen( 'myFile_updated.txt', 'w' ) ;
fwrite( fId, content ) ;
fclose( fId ) ;
where we use a regular expression for pattern matching. The call is
newContent = regexprep( oldContent, pattern, replacement ) ;
and the pattern matches a string
  • starting with the literal TABLE:
  • plus one or more + white space(s) \s
  • plus the literal "MASS SOURCE"
  • plus as few characters as possible .*? (lazy quantifier, when the greedy version .* means as many as possible)
  • and then followed by (but not replaced, it's a look forward (?=...) ) one or more carriage return or line feed |[\r
  7 Comments
Cedric
Cedric on 22 Sep 2017
Awesome! Please accept the answer if it helped.

Sign in to comment.

More Answers (0)

Categories

Find more on Text Data Preparation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!