Reputation: 1447
Previously, my java package declarations all start with 'zn' like "package zn.tree". Now after I changed the java folder path to remove 'zn' folder, I also want to change all java declarations to remove the prefix 'zn.' in a batch way. For example, I want change "package zn.tree" to "package tree".
So I decided to use bash script to change all the java files. After I googled, I wrote a script to use 'sed' command to do this job. However, it doesn't work and reports error. I still don't get familiar with the regex in 'sed'. My code is shown below:
#! /bin/bash
# change package declaration in batch way
for path in $(find $1 -type f -name "*.java"); do
sed -i "" 's/\<zn/.\>//g' $path
done
Hope someone could give me some clues. Thank you.
Upvotes: 3
Views: 1970
Reputation: 5519
Using the refactoring feature of an IDE such as Eclipse, IntelliJ or Netbeans will save you a lot of time for those needs. In IntelliJ, you would just create your new package and drag-and-drop your classes from the old package to the new one.
Upvotes: 0
Reputation: 201487
If I understand your question, you could do it with a one liner like,
find . -type f -name '*.java' -exec sed -i 's/^package zn\./package /1' {} \;
That will execute the sed
command and instruct it to edit in-place on every matching file. Note that I assume you want to match the first line starting with "package zn." and replace it with "package " once.
Upvotes: 4