Assuming that the identifiers - (source)
and (destination)
always appear at the end of the lines after the town names that are to be detected, see if this works for you -
%%// input_filepath and output_filepath are filepaths
%%// of input and output text files
str1 = importdata(input_filepath)
split1 = regexp(str1,'\s','Split')
%%// Store row numbers that do not have (source) or (destination) as a string
ind1 = ~cellfun(@isempty,(regexp(str1,'(source)'))) | ...
~cellfun(@isempty,(regexp(str1,'(destination)')));
str1 = strrep(str1,' (source)','')
str1 = strrep(str1,' (destination)','')
STRING = str1(ind1,:)
%%// Save as a text file
fid = fopen(output_filepath,'w');
for k = 1:size(STRING,1)
fprintf(fid,'%s\n',STRING{k,:});
end
fclose(fid);