generate tabular output from an input text file in unix shell scripting

Hi,

I have the output (as below) which i want it to be in a table.

For e.g.

space utilization in PSE on path /logs is 0%
space utilization in PSE on path /logs/tuxedo/tuxlsp is 16%
space utilization in PSE on path /ldvarlsp/lsp/log is 37%
space utilization in PSE on path /home is 6%
space utilization in MOCK2 on path /logs is 2%
space utilization in MOCK2 on path /logs/tuxedo/tuxlsp is 85%

I want the output to be in table format say where 1st column is the server name (here PSE/MOCK2), 2nd column is path name, 3rd column is space utilization and so on.

in my code.. name of server, path name and space utlization value is stored in a variable.

Table can be with simple border.

Pleaseeeeeeee help....

Thanks in Advance :slight_smile:

PK

kent$ cat t.txt
space utilization in PSE on path /logs is 0%
space utilization in PSE on path /logs/tuxedo/tuxlsp is 16%
space utilization in PSE on path /ldvarlsp/lsp/log is 37%
space utilization in PSE on path /home is 6%
space utilization in MOCK2 on path /logs is 2%
space utilization in MOCK2 on path /logs/tuxedo/tuxlsp is 85%

kent$ awk '{print "|"$4"#|"$7"#|"$9"# |"}' t.txt|column -s"#" -t |awk 'BEGIN{line=""}{l=length($0);if(length(line)==0)for(i=1;i<l;i++)line=line"-";print line;print $0}END{print line}'
-------------------------------------
|PSE    |/logs                |0%    |
-------------------------------------
|PSE    |/logs/tuxedo/tuxlsp  |16%   |
-------------------------------------
|PSE    |/ldvarlsp/lsp/log    |37%   |
-------------------------------------
|PSE    |/home                |6%    |
-------------------------------------
|MOCK2  |/logs                |2%    |
-------------------------------------
|MOCK2  |/logs/tuxedo/tuxlsp  |85%   |
-------------------------------------


 $ ruby -ne 'print $_.gsub(/space.*in\s+|on.*path\s+|is\s+/," | ")' file
 | PSE  | /logs  | 0%
 | PSE  | /logs/tuxedo/tuxlsp  | 16%
 | PSE  | /ldvarlsp/lsp/log  | 37%
 | PSE  | /home  | 6%
 | MOCK2  | /logs  | 2%
 | MOCK2  | /logs/tuxedo/tuxlsp  | 85%


A ksh:

while read k k k serv  k k path k sp
do
   printf "|%-6s|%25s|%4s|\n" "${serv}" "${path}" "${sp}"
done<infile

a bit verbose, but generic and configurable....
nawk -v f='4 7 9' -f pk.awk myFile
pk.awk:

BEGIN {
  pad=2
  if (!f) f="4 7 9"
  fn=split(f, fa, FS)
}
{
  for(i=1; i in fa;i++) {
    a[FNR]=(i==1)?$fa:a[FNR] OFS $fa
    w=(w>length($fa))?w:length($fa)
  }
}
END {
  for(i=1;i in a;i++) {
    n=split(a,tA, OFS)
    for(j=1;j in tA;j++)
       printf("%-*s%c", w[j]+pad, tA[j], (j==n)?ORS:"")
  }
}

Something a bit simpler (but not as sophisticated):

awk '{printf "%-15s|%-32s|%4s\n",$4,$7,$9}' file

Hi All... thanks for all your amazing replies :slight_smile:

@kato - ur code is working absolutely fine :slight_smile:

@sk1418 - i liked ur code for the output format... but when i tried to use it in my script it is giving error for "column -s" command

error diaplayed is :
./log_test4.ksh[23]: column: not found

i also tried to man column but it seems it is not supported here

pht025a2:/home/oper/pk264w/log_script> man column
No manual entry for column.

This is what i used...

awk '{print "|"$4"#|"$7"#|"$9"# |"}' mail_log_status.log | column -s"#" -t | awk 'BEGIN{line=""}{l=length($0);if(length(line)
==0)for(i=1;i<l;i++)line=line"-";print line;print $0}END{print line}'

Can You please halp me again :slight_smile:

Thanks in Advance :slight_smile:

You may not have column installed. It can handle the white spaces and give pretty output in your case. It saves a lot calculation too. You may want to install it or try others solutions.