Issue:
I write relatively complex functions where I have to make up the code with spaces.
It is expected that the first line with the content (non-space character) will be determined, N spaces will be cut from it UP to the first non-space character.
Then, in all subsequent lines, the previously determined N-th number of spaces will also be cut out.
Expectation examples:
Let's say there is a certain print
function.
In all subsequent cases, my expectation is:
this is the first line,
this is the second line
this is the third line.
Example of the cases used:
print '
this is line one
this is line two
this is line three
'
print '
this is line one
this is line two
this is line three
'
tmp() {
tmp2() {
print '
this is line one
this is line two
this is line three
'
}
tmp2
}
tmp
UPD: based on the practice of analyzing the answers to the issue, I added another one test:
tmp() {
tmp2() {
print '
this
this is line two
this is line three
'
}
tmp2
}
tmp
P.S.: My developments (works incorrectly)
print() {
INPUT_STRING=$1
# https://stackoverflow.com/questions/41010371/bash-counting-letters-in-string-output-always-a-little-different
COUNT_SPACES_BEFORE_TEXT=$(printf '%s' "${INPUT_STRING}" | grep -o '[^\n]*' | wc -l)
# https://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash
STRING_SPACES=$(printf "%${COUNT_SPACES_BEFORE_TEXT}s" |tr " " " ")
# https://stackoverflow.com/questions/23929235/multi-line-string-with-extra-space-preserved-indentation
EXPR_SED='1d;$d;'
EXPR_SED+="s/^${STRING_SPACES}//g"
printf '%s' "${INPUT_STRING}" | sed "${EXPR_SED}"
}
Please tell me how to do it?
P.S.S.: please note that the input and output are strictly showed in the question.
It is not necessary to offer "similar" solutions like:
printf '%s\n' \
"this is line one" \
"this is line two" \
"this is line three"
No need to offer an analysis of my programming style, etc.
The entrance and exit are clearly indicated. I am going to ignore the answers/comments not on the subject