Hey I try to write a littel bash script. This should copy a dir and all files in it. Then it should search each file and dir in this copied dir for a String (e.g @ForTestingOnly) and then this save the line number. Then it should go on and count each { and } as soon as the number is equals it should save againg the line number. => it should delete all the lines between this 2 numbers.
I’m trying to make a script which searchs for all this annotations and then delete the method which is directly after this ano.
Thx for help…
so far I have:
echo "please enter dir"
read dir
newdir="$dir""_final"
cp -r $dir $newdir
cd $newdir
grep -lr -E '@ForTestingOnly' * | xargs sed -i 's/@ForTestingOnly//g'
now with grep I can search and replace the @ForTestingOnly anot. but I like to delete this and the following method…
Give this a try. It’s oblivious to braces in comments and literals, though, as David Gelhar warned. It only finds and deletes the first occurrence of the “@ForTestingOnly” block (under the assumption that there will only be one anyway).
Edit: Removed one call to
sed(by commenting out and replacing a few lines).Edit 2:
Here’s a breakdown of the main
sedline:-n– only print lines when explicitly requested/@ForTestingOnly/,$– from the line containing “@ForTestingOnly” to the end of the files/ ... / ... /gperform a global (per-line) substitution\( ... \)– capture[{}]– the characters that appear in the list bewteen the square brackets\1\n– substitute what was captured plus a newlineta– if a substitution was made, branch to label “a”b– branch (no label means “to the end and begin the per-line cycle again for the next line) – this branch functions as an “else” for theta, I could have usedTinstead ofta;b;:a, but some versions ofseddon’t supportT:a– label “a”p– print the line (actually, print the pattern buffer which now consists of possibly multiple lines with a “{” or “}” on each one)=– print the current line number of the input fileThe second
sedcommand simply says to delete the lines starting at the one that has the target string and ending at the line found by thewhileloop.The
sedcommand at the top which I commented out says to find the target string and print the line number it’s on and quit. That line isn’t necessary since the mainsedcommand is taking care of starting in the right place.The inner
whileloop looks at the output of the mainsedcommand and increments counters for each brace. When the counts match it stops.The outer
whileloop steps through all the files in the current directory.