📄 pgrep.m
字号:
function pgrep(expression,string,options)%PGREP change strings within files%%PGREP(EXPRESSION,STRING,TYPE)% EXPRESSION: regular expression% STRING: the string to be searched.% TYPE: 1 - list file names, row number and line text% 2 - list file names and line text% 3 - list file names% 4 - as 1, but look for Matlab variables only% 5 - as 1, but span all subdirectories%%see also PSED%%Author: Federico Milano%Date: 11-Nov-2002%Update: 18-Feb-2003%Update: 30-Mar-2004%Version: 1.0.2%%E-mail: fmilano@thunderbox.uwaterloo.ca%Web-site: http://thunderbox.uwaterloo.ca/~fmilano%% Copyright (C) 2002-2005 Federico Milano%% This toolbox is free software; you can redistribute it and/or modify% it under the terms of the GNU General Public License as published by% the Free Software Foundation; either version 2.0 of the License, or% (at your option) any later version.%% This toolbox is distributed in the hope that it will be useful, but% WITHOUT ANY WARRANTY; without even the implied warranty of% MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU% General Public License for more details.%% You should have received a copy of the GNU General Public License% along with this toolbox; if not, write to the Free Software% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,% USA.if nargin < 3, disp('Check synthax ...') disp(' ') help pgrep, return,endif ~ischar(expression) disp('First argument has to be a not empty string.') returnendif ~ischar(string) | isempty(string) disp('Second argument has to be a not empty string.') returnendif ~isnumeric(options) | length(options) > 1 | rem(options,1) | ... options > 5 | options <= 0 disp('Third argument has to be a scalar integer within [1-5]') returnenda = dir(expression);file = {a.name}';a = dir(pwd);names = {a.name}';isdir = [a.isdir]';if options == 5, disp(' ') disp('Spanning directories ...') disp(' ') disp(pwd) file = deepdir(expression,names,file,isdir,pwd); options = 1;else disp(' ') disp(pwd)endn_file = length(file);if ~n_file, disp('No matches.'), return,endcheck = 0;try, if exist('strfind'); check = 1; endcatch % nothing to do % this for Octave compatibilityenddisp(' ')disp('Scanning files ...')disp(' ')for i = 1:n_file fid = fopen(file{i}, 'rt'); n_row = 0; while 1 & fid > 0 sline = fgets(fid); if ~isempty(sline), if sline == -1, break; end end n_row = n_row + 1; if check vec = strfind(sline,string); else vec = findstr(sline,string); end if ~isempty(vec) switch options case 1 disp([fvar(file{i},14),' row ',fvar(int2str(n_row),5), ... ' >> ',sline(1:end-1)]) case 3 disp(file{i}) break case 2 disp([fvar(file{i},14),' >> ',sline(1:end-1)]) case 4 okdisp = 0; okdispl = 0; okdispr = 0; for j = 1:length(vec) if vec(j) > 1, ch_l = double(sline(vec(j)-1)); if ch_l ~= 34 & ch_l ~= 39 & ch_l ~= 95 & ch_l ~= 46 & ... (ch_l < 48 | (ch_l > 57 & ch_l < 65) | ... (ch_l > 90 & ch_l < 97) | ch_l > 122) okdispl = 1; end end if vec(j) + length(string) < length(sline), ch_r = double(sline(vec(j)+length(string))); if ch_r ~= 34 & ch_r ~= 95 & ... (ch_r < 48 | (ch_r > 57 & ch_r < 65) | ... (ch_r > 90 & ch_r < 97) | ch_r > 122) okdispr = 1; end else okdispr = 1; end if okdispl & okdispr, okdisp = 1; break end okdispl = 0; okdispr = 0; end if okdisp, disp([fvar(file{i},14),' row ',fvar(int2str(n_row),5), ... ' >> ',sline(1:end-1)]), end end end end if fid > 0, count = fclose(fid); endend% --------------------------------------------------------------------------% find subdirectories% --------------------------------------------------------------------------function file = deepdir(expression,names,file,isdir,folder)idx = find(isdir);for i = 3:length(idx) disp([folder,filesep,names{idx(i)}]) newfolder = [folder,filesep,names{idx(i)}]; b = dir([newfolder,filesep,expression]); if ~isempty({b.name}), file = [file; strcat(folder,filesep,names{idx(i)},filesep,{b.name}')]; end b = dir([newfolder]); newdir = [b.isdir]'; newnames = {b.name}'; file = deepdir(expression,newnames,file,newdir,newfolder);end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -