If we intend to replace keyword '<abc>' with '<def>' in all *.java files from $ROOT_PATH, we can simply achieve this by:
The first part "find . -name "*.java" -print" will print all the relative paths of files which matches the pattern "*.java", just like:
Then all the output lines will be passed to sed command via xargs, thus the equivalent of the latter part is as below:
'-i' stands for:
which will output the replaced file to a file.
But here's a tricky part. As mentioned above, the '-i' is optional on Ubuntu, whereas it is kind of mandatory to give '-i' a value in Mac osx, or some error like 'invalid command code .' could be thrown. Consequently, it is recommend that we add '-i ""' to our command, although it's a little bit cumbersome :)
© 2014-2017 jason4zhu.blogspot.com All Rights Reserved
If transfering, please annotate the origin: Jason4Zhu
find . -name "*.java" -print | xargs sed -i "" "s/<abc>/<def>/g"
The first part "find . -name "*.java" -print" will print all the relative paths of files which matches the pattern "*.java", just like:
./a.java ./dir1/b.java
Then all the output lines will be passed to sed command via xargs, thus the equivalent of the latter part is as below:
sed -i "" "s/<abc>/<def>/g" ./a.java sed -i "" "s/<abc>/<def>/g" ./dir1/b.java
'-i' stands for:
-i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied)
which will output the replaced file to a file.
But here's a tricky part. As mentioned above, the '-i' is optional on Ubuntu, whereas it is kind of mandatory to give '-i' a value in Mac osx, or some error like 'invalid command code .' could be thrown. Consequently, it is recommend that we add '-i ""' to our command, although it's a little bit cumbersome :)
© 2014-2017 jason4zhu.blogspot.com All Rights Reserved
If transfering, please annotate the origin: Jason4Zhu
No comments:
Post a Comment