Replacing a specific line within a large .cmd file
10 views (last 30 days)
Show older comments
I need to replace specific lines within a large command file for a DOE. I assume I can read in the file and then replace the givin line (for example line 14549) with 'Replacement Text'. I am still realatively new to coding and don't know how to efficiently do this. I need to do this 14,641 times to create 14,641 files within a script (they will be deleted after being used). I'm guessing it will look something like this?
file = fopen('file.cmd','w','a')
command to replace line 14549 with 'newtextA'
command to replace line 15650 with 'newtextB'
command to replace line 18500 with 'newtextC'
fclose(file)
Thanks for your help!
Skeets
1 Comment
dpb
on 17 Jan 2014
Well, the problem with sequential files is that they are, well, "sequential". There is no such command as "replace line 14549 with 'newtextA'".
Your choices are to either
a) read the file sequentially and write that to a temporary file until the desired new line, write that new text to the temp file and ignore the text from the old one for the line then complete the rest of the file. Rename the temp to the old if want it to look like the operation was done in place.
b) Read the whole file into memory as string array and do the exchange in memory and then rewrite the modified text.
With many batch editors you can control such operations with external command sequence that is much less effort if you're familiar with any of the more commonly used programmers' editors.
Answers (1)
Jack
on 18 Jan 2014
First off make a back up of your file, it is extremely easy to mess it up while testing low level file IO.
Since your file is not delimited data and instead is a bunch of code I think the best way to go about this is try to get all your data in a string, then find the line you want to replace, then replace that line in the string and then write a new file. I took the liberty to write you a program to do just that. You must type in the name of your file in my code but other than that it should work everywhere except the last line of the command file. It will loop as many times as you tell it to before writing your new file. As far as doing that 14,000 times I would come up with an excel spreadsheet with the replacement text and line number and do it that way.
I copied the program in here but it doesn't show up very well so I'll attach the m file too.
clear
clc
%opening the file
file_data=fopen('file.cmd','r');
%getting data from file
my_text=fscanf(file_data, '%c')
%initializing the loop
change_another_line=true;
while change_another_line
%asking the user for which line to change
line_number_to_be_changed=input('Which line number needs to be replaced?: ');
replacement_string=input('What should go in it''s place?: ','s');
%converting string to ascii a 13 then a 10 means enter
ASCII_my_text=double(my_text)
%getting the size of the string
[~,cols]=size(ASCII_my_text)
%initializing the line number variable
line_num=1;
character_place=1;%row in the string where the line starts
for index=1:cols %finding where the enter is and counting it
if ASCII_my_text(index)==13 && ASCII_my_text(index+1)==10 %found the enter
line_num=line_num+1; %at the end of the line
character_place(line_num)=index+1; %first row of the line
end
end
character_place;
new_string=[my_text(1:character_place(line_number_to_be_changed)),...
replacement_string,...
my_text(character_place(line_number_to_be_changed+1)-1:cols)]
my_text=new_string; %updating the changed text
yes_or_no=input('Need to change another line? y or n: ','s');
if yes_or_no=='n' || yes_or_no=='N'
change_another_line=false; %ends the loop and then writes the file
end
end %end the while loop
%writing the new file
new_file_id=fopen('my_new_file.cmd','w');
fwrite(new_file_id,new_string);
%closing the file we just wrote
fclose(new_file_id);
%closing the original file
fclose(file_data);
0 Comments
See Also
Categories
Find more on Characters and Strings 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!