0
if [ $(grep -c 'Health Status: RED' $LOG) -gt 0 ]; then
    $(grep 'Server:' $LOG > $TMP )
    while read -r line
    do
        echo "$instance,1,$line"
    done < "$TMP"

    else
    echo "\n$instance,0,Health Status: GREEN\n"
fi

The output of above code is as follows:

Instance1,1,Server: EMEA

Instance1,1,Server: NAM

Instance1,1,Server: ASIA

Instance1,1,Server: AUSTRALIA

I need to add incremental space in $instance variable per line, add one space after $instance like below. Please note number of lines are not fixed.

Instance1 ,1,Server: EMEA  ==> One blank space added after Instance1

Instance1  ,1,Server: NAM ==> two blank spaces added after Instance1

Instance1   ,1,Server: ASIA ==> three blank spaces added after Instance1

Instance1    ,1,Server: AUSTRALIA ==> four blank spaces added after Instance1

any inputs would be greatly appreciated.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
SSA
  • 25
  • 1
  • 6
  • If you feed your snippet to https://www.shellcheck.net, it'll tell you a lot of useful things. I can see these: `[ $(grep -c ...) -gt 0 ]` instead of `grep -q ...`; multiple unquoted command substitutions; command substitution where it doesn't make sense; lots of unquoted variables; `echo` with escape characters in the string; useless temporary file instead of process substitution. – Benjamin W. Oct 18 '18 at 20:47
  • Thansk for looking into it, I agree to what you are saying I being novice coder and will try to refine the code later, for now its giving me the desired output, just need some inputs on how can I add blank spaces. – SSA Oct 18 '18 at 20:54
  • 1
    What is `$(grep 'Server:' $LOG > $TMP )` for? You are executing grep, writing output to a file, then taking the null string and trying to execute it. Remove the `$()` around the grep. – William Pursell Oct 18 '18 at 22:42
  • Instead of `if [ $(grep -c 'Health Status: RED' $LOG) -gt 0 ]`, just write `if grep -q 'Health Status: Red' "$LOG"; then ...` – William Pursell Oct 18 '18 at 22:43

2 Answers2

0

Try to use printf like below

for ((i=3;i<10;i++)); do
   printf "%*s\n" $i ",1,"
done
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

Replace your while, with this:

spaces_counter=1
while read -r line
do
    echo "$instance$(printf "%0.s " $(seq 1 $spaces_counter)),1,$line"
    (( spaces_counter += 1 ))
done < "$TMP"

Some explanations:

  • $(seq 1 $spaces_counter): outputs the integers from 1 to $spaces_counter
  • $(printf "%0.s " $(seq 1 $spaces_counter)): uses the seq from above to print a certain number of spaces.
  • the character after "%0.s " is where you specify the character you want repeated.

This is based on this answer: Print a character repeatedly in bash

combined with your code.

Nic3500
  • 8,144
  • 10
  • 29
  • 40