and print the number of dots (or any random characters) representing the depth at the beginning of each line the shell script should have 3 parameters. The first is character c, the second is the number of characters per level n, the third is the input file. The script should replace leading spaces with with k*n characters c where k is the level of bracket nesting.
a ( b
c d [ e ] f [
g h { j (
k ) } l m
n ] o ) p
q r
would be modified as follows if the script is run with parameters c='.' and n=1:
a ( b
.c d [ e ] f [
..g h { j (
....k ) } l m
..n ] o ) p
q r
Here is my attempt:
c=$1
sed 's|^[[:blank:]]*||g' $3
curr=0
next=0
nb=0
n=$2
makeIndent() {
local indentChar=$1
local num=$2
printf '%*s' "$num" | tr ' ' "$indentChar"
}
while read -r line; do
for char in '(' '[' '{'; do
nb=$((curr+1))
next=$((next+nb))
done
for char in ')'']' '}'; do
nb=$((curr-1))
next=$((next-nb))
done
n=$(($n*$curr))
indentString=$(makeIndent "$c" "$n")
curr=$next
n=$2
echo "$indentString$line"
done < $3
How can I delete the leading spaces without printing something like this?
a(b
c d[ e]f [
g h { j (
k)}l m
n ]o )p
q r
and the next thing it prints is (with c is '.' and n is 1)
a(b
.....c d[ e]f [
...............g h { j (
...................................k)}l m
...........................................................................n ]o )p
that means the last line was deleted. How can I keep the last line as original? And the counting part is wrong. So I tried to find some ways to count the depth such as creating a count function and change the for loop a little bit
count() {
local a=$1
local file=$2
awk -F\$a '{ print NF-1 }' $file
}
for char in '(' '[' '{'; do
nb=$(count "$char" "$3")
next=$((next+nb))
done
for char in ')'']' '}'; do
nb=$(count "$char" "$3")
next=$((next-nb))
done
It turns out to be even worse.