How Can I replace specific lines in a text file

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

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.
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

Thank you very much for your quick response, I will try to work on it
My pleasure. I had a quick look at it and if I understand well, you want to
  1. Extract the table name "VEHICLES 3 - GENERAL VEHICLES 2 - LOADS" from the second file
  2. Find the corresponding table in the first file
  3. Replace the content from first file with content from second file.
If this is it, here is one way based on what I just explained:
originalFile = 'Original text.$2k.txt' ;
replacementFile = 'Text to replace.$2k.txt' ;
outputFile = 'Output.txt' ;
% - Extract table name from replacement file.
replacementContent = fileread( replacementFile ) ;
tableName = regexp( replacementContent, '(?<=TABLE:\s+")[^"]+', 'match', 'once' ) ;
% - Build pattern for matching what to replaced, based on the table name.
pattern = sprintf( 'TABLE:\\s+"%s.*?(?=[\\r\\n]TABLE)', tableName ) ;
% - Replace in original content.
originalContent = fileread( originalFile ) ;
originalContent = regexprep( originalContent, pattern, replacementContent ) ;
% - Export updated content.
fId = fopen( outputFile, 'w' ) ;
fwrite( fId, originalContent ) ;
fclose( fId ) ;
We extract the name by
  • picking as many characters different from " as possible (not in the set of just " : [^'] )
  • that follow the literal TABLE: followed by one or more white spaces followed by the literal ": (?<=...) is a look backward/behind, it tells the regexp engine to match something that is preceded by what is defined by the ....
Then we build the second pattern based on the name with SPRINTF. The only extra difficulty with respect to my first solution is that \ is a special character for SPRINTF ( formatSpec ), and must be doubled (because you want to produce the character \ in the pattern.
Finally, note that it is easy to loop through replacement files if you have many of them:
originalFile = 'Original text.$2k.txt' ;
replacementFiles = {'Rep1.txt', 'Rep2.txt', ...} ; % or the output of DIR.
outputFile = 'Output.txt' ;
originalContent = fileread( originalFile ) ;
for rfId = 1 : numel( replacementFiles )
replacementContent = fileread( replacementFiles{rfId} ) ;
tableName = regexp( replacementContent, '(?<=TABLE:\s+")[^"]+', 'match', 'once' ) ;
pattern = sprintf( 'TABLE:\\s+"%s.*?(?=[\\r\\n]TABLE)', tableName ) ;
originalContent = regexprep( originalContent, pattern, replacementContent ) ;
end
fId = fopen( outputFile, 'w' ) ;
fwrite( fId, originalContent ) ;
fclose( fId ) ;
It is very great now, But I still have one problem. The replacement file might have many tables as well. I do not want to place the whole replacement file into the original one. Rather, I need to place a specific table. I'm attaching an updated text files. Thank you very much
I updated it for you, because it is starting to be a little more complex:
originalFile = 'Original text.$2k.txt' ;
replacementFile = 'Text to replace.$2k.txt' ;
outputFile = 'Output2.txt' ;
% - Read files, extract tables plus their content from replacement.
originalContent = fileread( originalFile ) ;
replacementContent = fileread( replacementFile ) ;
tables = regexp( replacementContent, 'TABLE.*?(?=\s*(TABLE|$))', 'match' ) ;
% - Iterate through replacements and update original content.
for pId = 1 : numel( tables )
% - Extract table name from replacement.
tableName = regexp( tables{pId}, '(?<=")[^"]+', 'match', 'once' ) ;
% - Build pattern for matching what to replace, based on the table name.
pattern = sprintf( 'TABLE:\\s+"%s.*?(?=\\s*(TABLE|END))', tableName ) ;
% - Replace in original content.
originalContent = regexprep( originalContent, pattern, ...
strtrim( tables{pId} )) ;
end
% - Export updated content.
fId = fopen( outputFile, 'w' ) ;
fwrite( fId, originalContent ) ;
fclose( fId ) ;
If you are facing many of these situations and think that investing in regular expressions for text pattern matching could be profitable, Mathworks wrote a nice section about them and I give the reference in the PS of this thread.
Thank you very much, your help is very appreciated. The code is running great now.
The only thing I modified, I wrote each different table in a separate text file to avoid looping and it worked very well
Awesome! Please accept the answer if it helped.
Thanks for this wonderful answer.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 22 Sep 2017

Commented:

on 30 Nov 2019

Community Treasure Hunt

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

Start Hunting!