Formatting my output

Dear All,

I am new to unix scripting. I need your help to format my output on screen.

echo "        --------------------------------------------"
echo "        |                My Output                  |"
echo "        --------------------------------------------"
echo "        | A: $A  | B:$B | C:$C | D:$D   |"
echo "        | E: $E  | F:$F | G:$G | H:$H   |"
                --------------------------------------------
                |                My Output                 |
                --------------------------------------------
                | A: 11  | B:17 | C:2 | D:3   |
                | E: 2  | F:13262 | G:2 | H:1   |

i need your help to properly display the above result without disturbing the template.

awkscrp:

BEGIN{
        printf("%s\n%s\n%s\n","--------------------------------------------", "\t| My Output |\t", " -------------------------------------------" )
        printf("%s%d%s%d%s%d%s%d%s\n", " | A:", a ,"| B:", b ,"| C:", c , " | D:", d , " |") ;
        printf("%s%s%s%s%s%s%s%s%s\n", " | E:", e ,"| F:", f ,"| G:", g , " | H:", h , " |")
}

Run as:

awk -f awkscrp -v a="$A" -v b="$B" -v c="$C" -v d="$D" -v e="$E" -v f="$F" -v g="$G" -v h="$H"

cheers,
Devaraj Takhellambam

I have created below script but it is giving me error.

A=11
B=17
C=2
D=3
E=2
F=13262
G=25
H=4569

BEGIN{
        printf("%s\n%s\n%s\n","--------------------------------------------", "\t| My Output |\t", " -------------------------------------------" )
        printf("%s%d%s%d%s%d%s%d%s\n", " | A:", a ,"| B:", b ,"| C:", c , " | D:", d , " |") ;
        printf("%s%s%s%s%s%s%s%s%s\n", " | E:", e ,"| F:", f ,"| G:", g , " | H:", h , " |")
}

awk -f awkscrp -v a="$A" -v b="$B" -v c="$C" -v d="$D" -v e="$E" -v f="$F" -v g="$G" -v h="$H"

when i run this script through sh shell it gives me below result.

Please advice

  1. Create the below awk script and give execute perm.
    awkscrp:
BEGIN{
        printf("%20s\n%s\n%20s\n","-----------------------------------------------------------",
               "\t\t\t| My Output |\t\t\t", " ----------------------------------------------------------" )
        printf("%s%+10s%s%+10s%s%+10s%s%+10s%s\n", " | A:", a ,"| B:", b ,"| C:", c , " | D:", d , " |") ;
        printf("%s%+10s%s%+10s%s%+10s%s%+10s%s\n", " | E:", e ,"| F:", f ,"| G:", g , " | H:", h , " |")
}
  1. Create the below file as a script to be executed.

format.sh

#!/bin/ksh

A=11
B=17
C=2
D=3
E=2
F=13262
G=25
H=4569
awk -f awkscrp -v a="$A" -v b="$B" -v c="$C" -v d="$D" -v e="$E" -v f="$F" -v g="$G" -v h="$H"
  1. run format.sh

cheers,
Devaraj Takhellambam

Awk isn't required.

A=11
B=17
C=2
D=3
E=2
F=13262
G=25
H=4569

echo "        --------------------------------------------"
echo "        |                My Output                  |"
echo "        --------------------------------------------"
printf "        | A: %5s  | B:%5s | C:%5s | D:%5s   |\n" $A $B $C $D
printf "        | E: %5s  | F:%5s | G:%5s | H:%5s   |\n" $E $F $G $H

Both are working fine!

Thanks alot for the help :slight_smile:

Cheers.