Well, I had free time on hand, so I decided to relive my good old linux days:]
After reading a bit about awk and sed, I decided that it would be better to use both, as it is easier to add indentation to awk and syntax lines to sed.
Here is the ~ / sed_script that formats the source file:
# delete indentation
s / ^ \ + // g
# format lines with class
s /^\(.\+ class. \ + \) * \ ({. * \) $ / \ 1 \ n \ 2 / g
# format lines with methods
s / ^ \ (public \ | private \) \ (\ + static \) \? \ (\ + void \) \? \ + \ (. \ + (. *) \) * \ ({. * \) $ / \ 1 \ 2 \ 3 \ 4 \ n \ 5 / g
# format lines with other structures
/ ^ \ (if \ | else \ | for \ | while \ | case \ | do \ | try \) \ ([^ {] * \) $ /, + 1 {# get lines not containing '{'
# along with the next line
/.*{.*/ d # delete the next line with '{'
s / \ ([^ {] * \) / \ 1 {/ g # and add '{' to the first line
} And here is ~ / awk_script that adds padding:
BEGIN {depth = 0}
/} / {depth = depth - 1}
{
getPrefix (depth)
print prefix $ 0
}
/ {/ {depth = depth + 1}
function getPrefix (depth) {
prefix = ""
for (i = 0; i <depth; i ++) {prefix = prefix ""}
return prefix
} And you use them like this:
> sed -f ~ / sed_script ~ / file_to_format> ~ / .tmp_sed
> awk -f ~ / awk_script ~ / .tmp_sed This is far from the right formatting tool, but I hope it will do OK as a sample script for reference:] Good luck with your training.
Kirill Strizhak
source share