0
votes

I have a function in my script and i need it to display the output on a table with a header and hide the ligne that contain RELEASE on the output.

for file in ${list[@]}; do
  echo -ne ${file} ${ins} " "; env | grep -i ${file} | cut -f2 -d"=" 
done

here's the outpuit i get right now :

comp1 installed !  v0.6.2
comptest1 installed !  v2.1.1
comp2 installed !  v2.1.5
comp3 installed !  v0.12.0
1.1.18-RELEASE
comp4 installed !  v0.7.0

expected outpout :

Component  status         version
comp1      installed !    v0.6.2
comptest1  installed !    v2.1.1
comp2      installed !    v2.1.5
comp3      installed !    v0.12.0
comp4      installed !    v0.7.0

what i've tried :

i tried something like redirecting the output of the echo commant to a temp file and then using

column -t -s "," -o "||" temp.txt

But i didn't work i keep getting errors like No such file or directory.

I tried using printf and awk too

awk '{printf "%-30s|%-18s|%-20s\n",${file},${ins} " "; env | grep -i ${file} | cut -f2 -d"="}'

It didn't work too i keep getting syntax errors.

1
Have you considered using the built-in printf?mustaccio
What is your input?dawg
@mustaccio i tried but it didn't want to take in the grep -i (i'm new to scripting so i didn't know where the problem is i will add it to the questionTenEM
@dawg the input is the execution of the script the vars i hace in the echo command are just the list of components and their statusTenEM
Please add output of declare -p list to your question.Cyrus

1 Answers

1
votes

Here is an awk script using standard Linux gawk.

BEGIN { # run once before processing each line
  outputFormat = "%-12s|%-10s|%-8s\n"; # configure line output format
  printf (outputFormat, "Component", "Status", "Version"); # output headers
  print "---------------------------------"; # set header line
}
/-RELEASE/{next;} # ignore each line containing string "-RELEASE"
{ # each line
  printf(outputFormat, $1, $2, $4); # print formated fields 
}

The output

$ awk -f script.awk input.txt
Component   |Status    |Version
---------------------------------
comp1       |installed |v0.6.2
comptest1   |installed |v2.1.1
comp2       |installed |v2.1.5
comp3       |installed |v0.12.0
comp4       |installed |v0.7.0