Code refactoring is sometimes tedious, and string substitution is not only tedious, but also error-prone, with some tools available, PHP for example, REPHACTOR,SCISR and so on, but ready-made tools often mean inflexible So today I'm going to talk about the application of shell in code refactoring.
First of all, let's say we want to replace all the Foo_bar in the php file with Foobar, so we can do the following:
Method one, using sed:
shell> find/path-name "*.php" | Xargs sed s/foo_bar/foobar/g
Method two, using awk:
shell> find/path-name "*.php" | Xargs awk {gsub (/foo_bar/, "FooBar"); print;}
Note: For the sake of simplicity, I printed the results directly to the terminal screen, as to how to save, will be explained later.
Then it's complicated: Suppose a PHP project, before using a class, must call a method called "Includeclass", and now use the way the class is loaded automatically, so to remove hard-coded includeclass calls, for aesthetic reasons, If the Includeclass line below is empty, it is also deleted, taking into account the case insensitive factor.
Pre-Refactoring code example:
Includeclass (...);
echo A;
04
Echo b;
Includeclass (...);
Includeclass (...);
08
09
Ten echo C;
11
Echo D;
Includeclass (...);
14
15
echo e;
?>
Example of refactoring code:
echo A;
03
Echo b;
05
Echo c;
07
Echo D;
09
Ten echo E;
?>
Before we do this, we need to get a look at the approximate situation:
shell> grep-i-ri Includeclass/path | More
Among them, the parameters of the grep command at first glance is not easy to remember, but as long as I say the way to remember, will never forget: the previous parameters as English, the following parameters as pinyin. Refer to the man documentation for the exact meaning of the parameters.
Method one, use sed to write script script.sh:
#!/bin/sh
For PHP in $@; Do
/bin/sed-i
/includeclass/i {
H
D
}
/^$/ {
X
/includeclass/id
X
}
H
$PHP
Done
Note: The space is limited, I write the regular is relatively simple
The disadvantage of SED is that code readability is poor, and the advantage is that the code is short. In addition, the built-in "-i" option can be saved directly, which is one of the reasons I like sed.
Method Two, use awk to write script script.sh:
#!/bin/sh
For PHP in $@; Do
tmp=$ (mktemp)
/bin/awk
BEGIN {
IGNORECASE = 1
}
/includeclass/{
Previous = $
Next
}
/^$/ {
if (previous ~/includeclass/) {
Previous = $
Next
}
}
{
Previous = $
Print
}
$PHP > $TMP
/bin/cp-f $TMP $PHP
/bin/rm-f $TMP
Done
Note: The space is limited, I write the regular is relatively simple
The disadvantage of awk is that the code is long and the advantage is that the code is more readable. In addition, the program is created by generating a unique temporary file to complete the save.
Reminder: It is sometimes inappropriate to overwrite the original file directly, after all, there may be no thoughtful place, the use of SVN will not have such a concern, because even if the original file is overwritten, it can be submitted through the "SVN diff" command to check the correct error, even if it is submitted, you can revert to the previous version.
What if I call the script.sh script? Here's a general example:
shell> find/path-name "*.php" | xargs/path/to/script.sh
The simple task of using SED to write a very appropriate, complex task is best written in awk, Combat is the best way to learn, specifically, you can refer to sed one line and awk line and other information.
Description: Both sed and awk used in this article refer to the GNU version.
http://www.bkjia.com/PHPjc/478849.html www.bkjia.com true http://www.bkjia.com/PHPjc/478849.html techarticle code refactoring is sometimes tedious, and it's not only tedious and error-prone to do things like string substitution, but there are some tools available, in PHP, for example, Re ...