Leser: 27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/perl use strict; use warnings; my $filein ='/path/to/in/file.c'; my $fileout='/path/to/out/file.c'; open(my $fhin, '<', $filein) or die "ERROR open $filein ($!)\n"; open(my $fhout, '>', $fileout) or die "ERROR open $fileout ($!)\n"; while(my $line=<$fhin>) { # suchen und ersetzen $line=~s/(^\s*#\s*if\(\s*)MY_OLD_CONST\s*==\s*1(\s*\))/$1MY_NEW_CONST == 5$2/; print $fhout $line; } close($fhout); close($fhin);
Guest perltiptopwas bedeutet eigentlich /$1 bzw /$2 hier /$1MY_NEW_CONST == 5$2/ ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
[code=perl] #!/usr/bin/perl # Designed by # Version xx.xx.xx # # Function: # Input/Parameter: # Output: # USAGE: #------------------------------------------------------------------------------ # * Usings use warnings; use strict; use Cwd; # *----------------------------------------------------------------------------- # ------------------------------------------------------------------------------ # * Variables my $str_search_pattern = "MY_OLD_CONST"; my $str_replacement_expression = "MY_NEW_CONST"; my $logfileout='C:\temp\sysconst.log'; my $path; my $path_tbc; my $filein; my $fileout; #my@arr_all_files=Sub_Search_For_Files_REC(Sub_Get_Root_Directory(),1,'.xml'); my @arr_all_files=Sub_Search_For_Files_REC(Sub_Get_Root_Directory(),1); my @arr_files_tbc=(); my $st_files_tbc; my %hash_alloc_pp_key=('0','0','1','100','2','200','3','300','4','400','5','500'); foreach $path (@arr_all_files) { $st_files_tbc=0; $filein =$path; open(my $FHIN , '<', $filein) or die "ERROR open $filein ($!)\n"; while(my $line=<$FHIN>) { if($line=~/(^\s*#\s*(elif|if)\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*[!=]+=\s*(\d)(.*\))/) { $st_files_tbc=1; } elsif($line=~/(^\s*#\s*ifndef\s*[\(\s]*\s*\(*\s*)${str_search_pattern}(.*)/) { $st_files_tbc=2; } elsif($line=~/(^\s*#\s*define\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*(\d)(.*)/) { $st_files_tbc=4; } elsif($line=~/(^\s*#\s*error.*)${str_search_pattern}(\.*)/) { $st_files_tbc=8; } elsif($line=~/(^\s*\/\*\s*.*)${str_search_pattern}(.*)\*\//) { $st_files_tbc=16; } elsif($line =~/(\.*<ONE-XML-TAG>\s*)${str_search_pattern}(\s*<\/ONE-XML-TAG>\s*==\s*)(\d)/) { $st_files_tbc=32; } elsif($line =~/(\.*<Secound-XML-TAG>\s*)${str_search_pattern}(\s*<\/Secound-XML-TAG>)/) { $st_files_tbc=64; } elsif($line =~/(^\s*<\s*VT\s*>\s*)${str_search_pattern}(\s*<\s*\/VT\s*>)/) { $st_files_tbc=128; } elsif($line =~/(\s*<ONE-XML-TAG>\s*)${str_search_pattern}(\s*<\/ONE-XML-TAG>)/) { $st_files_tbc=255; } } if($st_files_tbc>0) { push (@arr_files_tbc,$path); } close($FHIN); } foreach $path_tbc (@arr_files_tbc) { $filein =$path_tbc; $fileout=$path_tbc.".keep"; open(my $FHIN , '<', $filein) or die "ERROR open $filein ($!)\n"; open(my $FHOUT, '>', $fileout) or die "ERROR open $fileout ($!)\n"; open(my $FHLOG, '>', $logfileout) or die "ERROR open $logfileout ($!)\n"; while(my $line=<$FHIN>) { # suchen und ersetzen vom PP-Schalter und dessen Wert in Dateien! if($line=~/(^\s*#\s*(elif|if)\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*[!=]+=\s*(\d)(.*\))/) { $line=~s/(^\s*#\s*(elif|if)\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*([!=]+=)\s*(\d)(.*\))/$1${str_replacement_expression}$3$hash_alloc_pp_key{$4}$5/; #if($line=~/(\s*(&||)\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*([!=]+=)\s*(\d)(.*\))/) #{ #$line=~s/(\s*(&||)\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*([!=]+=)\s*(\d)(.*\))/$1${str_replacement_expression}$3$hash_alloc_pp_key{$4}$5/g; #} while($line=~/(\s*(&||)\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*([!=]+=)\s*(\d)(.*\))/) { $line=~s/(\s*(&||)\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*([!=]+=)\s*(\d)(.*\))/$1${str_replacement_expression}$3$hash_alloc_pp_key{$4}$5/g; } print $FHLOG "Line number| \t" . "$." . "|\t" . "Path | ". "\t" . "$path_tbc " . "| \t" . "New Expression |" . "\t". "$line" . "\n" ; } elsif($line=~/(^\s*#\s*ifndef\s*[\(\s]*\s*\(*\s*)${str_search_pattern}(.*)/) { $line=~s/(^\s*#\s*ifndef\s*[\(\s]*\s*\(*\s*)${str_search_pattern}(.*)/$1${str_replacement_expression}$2/; print $FHLOG "Line number| \t" . "$." . "|\t" . "Path | ". "\t" . "$path_tbc " . "| \t" . "New Expression |" . "\t". "$line" . "\n"; } elsif($line=~/(^\s*#\s*define\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*(\d)(.*)/) { $line=~s/(^\s*#\s*define\s*[\(\s]+\s*[!]*\(*\s*)${str_search_pattern}\s*(\d)(.*)/$1${str_replacement_expression}$hash_alloc_pp_key{$2}$3/; print $FHLOG "Line number| \t" . "$." . "|\t" . "Path | ". "\t" . "$path_tbc " . "| \t" . "New Expression |" . "\t". "$line" . "\n"; } elsif($line=~/(^\s*#\s*error.*)${str_search_pattern}(\.*)/) { $line=~s/(^\s*#\s*error.*)${str_search_pattern}(\.*)/$1${str_replacement_expression}$2/; print $FHLOG "Line number| \t" . "$." . "|\t" . "Path | ". "\t" . "$path_tbc " . "| \t" . "New Expression |" . "\t". "$line" . "\n"; } elsif($line=~/(^\s*\/\*\s*.*)${str_search_pattern}(.*)\*\//) { $line=~s/(^\s*\/\*\s*.*)${str_search_pattern}(.*\*\/)/$1${str_replacement_expression}$2/; } elsif($line =~/(\.*<ONE-XML-TAG>\s*)${str_search_pattern}(\s*<\/ONE-XML-TAG>\s*==\s*)(\d)/) { $line =~s/(\.*<ONE-XML-TAG>\s*)${str_search_pattern}(\s*<\/ONE-XML-TAG>\s*==\s*)(\d)/$1${str_replacement_expression}$2$hash_alloc_pp_key{$3}/g ; print $FHLOG "Line number| \t" . "$." . "|\t" . "Path | ". "\t" . "$path_tbc " . "| \t" . "New Expression |" . "\t". "$line" . "\n"; } elsif($line =~/(\.*<Secound-XML-TAG>\s*)${str_search_pattern}(\s*<\/Secound-XML-TAG>)/) { $line =~s/(\.*<Secound-XML-TAG>\s*)${str_search_pattern}(\s*<\/Secound-XML-TAG>)/$1${str_replacement_expression}$2/g; print $FHLOG "Line number| \t" . "$." . "|\t" . "Path | ". "\t" . "$path_tbc " . "| \t" . "New Expression |" . "\t". "$line" . "\n"; } elsif($line =~/(^\s*<\s*VT\s*>\s*)${str_search_pattern}(\s*<\s*\/VT\s*>)/) { $line =~s/(^\s*<\s*VT\s*>\s*)${str_search_pattern}(\s*<\s*\/VT\s*>)/$1${str_replacement_expression}$2/g; print $FHLOG "Line number| \t" . "$." . "|\t" . "Path | ". "\t" . "$path_tbc " . "| \t" . "New Expression |" . "\t". "$line" . "\n"; } elsif($line =~/(\s*<ONE-XML-TAG>\s*)${str_search_pattern}(\s*<\/ONE-XML-TAG>)/) { $line =~s/(\s*<ONE-XML-TAG>\s*)${str_search_pattern}(\s*<\/ONE-XML-TAG>)/$1${str_replacement_expression}$2/; print $FHLOG "Line number| \t" . "$." . "|\t" . "Path | ". "\t" . "$path_tbc " . "| \t" . "New Expression |" . "\t". "$line" . "\n"; } print $FHOUT $line; } close($FHOUT); close($FHIN); close($FHLOG); } print join("\n",@arr_files_tbc); # # *************************************************************************************************** # * Sub_Get_Root_Directory # * # * Function # * # * # * \return root_directory # *************************************************************************************************** # */ sub Sub_Get_Root_Directory { # startverzeichnis wir ueber kommandozeile vom Anwender festgelegt. my $root_directory = $ARGV[0]; my $cmd_input ; if(@ARGV == 0) { # das aktuelle Verzeichnis ermitteln. $root_directory=cwd; print "\n################################################################################\n"; print "Start directorie is:\t $root_directory\n"; print "Do you want to continue?\nPress any key for continue or Press q for Quit:"; print "\n################################################################################\n"; chomp ($cmd_input = <STDIN>); if($cmd_input eq "q") { print "Script was aborted by user!\n"; exit; } } return $root_directory; } # *************************************************************************************************** # * Sub_Search_For_Files_REC # * # * Function # * Startdir ohne abschließendes /, aktuelles Verzeichnis = . # * Unterverzeichnisse durchsuchen? 1: ja, 0: nein # * Dateitypen in form .c.h.xml , also direkt hintereinander, aber nur wenn nötig # * ansonsten wird alles gezeigt # * \return Liste der gefundenen Dateien - @arr_all_files # *************************************************************************************************** sub Sub_Search_For_Files_REC { my $startdir=shift; my $include_subdirs=shift; my $strFileExtension=shift; my %strFileExtension=(); $strFileExtension=~ s/\s//g; my @strFileExtension=split('\.',$strFileExtension); shift @strFileExtension; my @arr_all_files=(); $strFileExtension=0; $startdir=~ s/\/$//; if (@strFileExtension != 0) { $strFileExtension=1; # wenn Endungen angegeben foreach (@strFileExtension){$strFileExtension{$_}=1;} } @strFileExtension=(); push (my @all_directories,$startdir); foreach my $akdir(@all_directories) { local *in; opendir (in,$akdir); my @all=readdir(in); closedir in; foreach my $akdatei (@all) { next if ($akdatei eq '..' || $akdatei eq '.'); if (-d "$akdir/$akdatei") { if ($include_subdirs == 1) { push (@all_directories,"$akdir/$akdatei"); next; } } else { if ($strFileExtension==0) { push (@arr_all_files,"$akdir/$akdatei"); } else { my @endung=split('\.',$akdatei); my $endung=$endung[-1]; if ($strFileExtension{$endung} == 1) { push (@arr_all_files,"$akdir/$akdatei"); } } } } } return @arr_all_files; } [code=perl]