Output in table format

I have one script which generate file called report.txt having following output

parameter_name status comment
banking ok NA
finance 30% hike NA
Loan_department ok 20%
HR_Group defaulters Ajay 

I wanted to convert this file into tabular form.
You can see each line contain 3 words and they are separated by single space.

So to covert it into table i use following command

awk '{printf "%-30s|%-18s|%-20s\n",$1,$2,$3}'  report.txt > final_report.txt

final_report.txt looks like

parameter_name                |status            |comment
banking                       |ok                |NA
finance                       |30%               |hike
Loan_department               |ok                |20%
HR_Group                      |defaulters        |Ajay

When i send this file to my mail using mailx command the columns get disturbed from their original position.
and output become difficult to read though on UNIX box it is wel formatted.

Is there any other way to convert this report.txt file into tabular form.
Also converted file should look good and undisturbed when I send that file to my mail ID by mailx command.

The result depends on how the receiving mail program displays a text only mail, some use a fixed width font, others a variable width font.

The solution for this issue is described here.

1 Like

you can use the below code to convert your data into HTML table

awk 'BEGIN {print "<table>"}
  {print "<tr>"; for(i = 1; i <= NF; i++) print "<td>" $i "</td>"; print "</tr>"}
  END {print "</table>"}' report.txt > report.html

and send this using 'sendmail' command provided by hergp

1 Like

Hi.

See also post #14 in thread Color encoding on the disk space script for a perl solution to create tables from text to formats in ASCII, HTML, and bbcode.

Best wishes ... cheers, drl

1 Like

Hi drl,
Thanks for your help
I saw your code in link provided by you.
I tried a few combinations. But probably i am doing something wrong.
Can you give me the code for above problem.? Also i am not sure whether PERL code can work on HP-UX server. I am not much familiar with perl.
your code that gives colorful output is excellent. Can you give me same for this problem.? i am using HP-UX server

Thanks in advance :slight_smile:

Hi, Nakul_sh.

The HP system to which I have access is:

OS, ker|rel, machine: HP-UX, B.11.11, 9000/785

and the perl version there is:

$ perl -v

This is perl, v5.10.1 (*) built for PA-RISC2.0
... 

Before we go any farther, please post the version of HP-UX you are using, and the version of perl on that machine ... cheers, drl

hi drl,
seems that perl is available for me but i am very new to perl scripting.
Can you give me the exact code to resolve this problem.? I have generated .html file by using the code provided by you.
Can you give me further code to convert HTML file into a colorful table?
I saw the user Chubler_XL have given a code in below link

But i am not able to convert this code to suit my requirement.
I will be thankful to you if you could help. :slight_smile:

Hi.

Observations:

1) The operations of creating text tables and of coloring strings are separate issues.

2) Learning any language takes practice. We can help you with solutions, but it is up to you to read materials and do exercises to learn. The perl classes with which I was associated were for experienced programmers and were intensive multi-day classes that had many hands-on exercises. I suggest you attend such a class or search for a tutorial that suits your abilities and goals.

Here is a script that runs on HP-UX as noted:

#!/usr/bin/env bash

# @(#) s1       Demonstrate creating ASCII tables and coloring element.
# See:
# http://www.andre-simon.de/zip/ansifilter-1.7.tar.bz2

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl ansifilter

# Function to color a string.
# call:  color string-to-be colored file
color() {
  escape=""     # in vi, use control-v, then control-[ to get ascii ESC
  red="${escape}[01;31m"
  restore="${escape}[00m"
  target=$1
  file=$2
  sed "s/$target/${red}&${restore}/" $file
}

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

rm -f f1 f2 f3.html
pl " Results, ASCIITable, ansifilter:"
tail -n+2 $FILE |
./p1 |
tee f1
ansifilter --bbcode f1 > f2
ansifilter --html f1 > f3.html

# Add a color to elements matching "defaulting"
color defaulters $FILE > data2

pl " Results, ansifilter with a color, console color:"
cat data2

rm -f f1 f2
pl " Results, ansifilter with a color, bbcode color:"
tail -n+2 data2 |
./p1 > f1
ansifilter --bbcode f1 > f2
cat f2

pl " Use of ASCIITable in perl script p1:"
cat p1

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: HP-UX, B.11.11, 9000/785
Distribution        : GenericSysName [HP Release B.11.11] (see /etc/issue)
GNU bash 4.2.37
perl 5.10.1
ansifilter - (local: ~/executable/ansifilter Apr 18 14:13 )

-----
 Input data file data1:
parameter_name status comment
banking ok NA
finance 30% hike NA
Loan_department ok 20%
HR_Group defaulters Ajay

-----
 Results, ASCIITable, ansifilter:
.----------------------------------------.
| parameter_name  | status     | comment |
+-----------------+------------+---------+
| banking         | ok         | NA      |
| Loan_department | ok         |     20% |
| HR_Group        | defaulters | Ajay    |
'-----------------+------------+---------'

-----
 Results, ansifilter with a color, console color:
parameter_name status comment
banking ok NA
finance 30% hike NA
Loan_department ok 20%
HR_Group defaulters Ajay

-----
 Results, ansifilter with a color, bbcode color:
.-----------------------------------------------------.
| parameter_name  | status                  | comment |
+-----------------+-------------------------+---------+
| banking         | ok                      | NA      |
| Loan_department | ok                      |     20% |
| HR_Group        | defaulters | Ajay    |
'-----------------+-------------------------+---------'

-----
 Use of ASCIITable in perl script p1:
#!/usr/bin/env perl

# @(#) p1       Demonstrate basic text table.

use Text::ASCIITable;

$t = Text::ASCIITable->new();
# $t->setCols( "c1", "c2", "c3", "c4", "c5", "c6", "c7" );
$t->setCols( "parameter_name", "status", "comment" );
# $t->setOptions({ hide_HeadRow => 1, hide_FirstLine => 1 });


while (<>) {
  @a = split;
  $t->addRow(@a);
}
print $t;

exit(0);

I obtained the source for ansifilter, compiled it on the HP, and executed it as noted in the script above.

There are many ways to add color to text strings. The sed solution above is relatively simple and straight-forward, but requires modification if you desire colors other than red.

Good luck ... cheers, drl

---------- Post updated at 14:28 ---------- Previous update was at 08:46 ----------

Hi.

Sorry, the sequence should have been create table, then apply color:

...
cp data1 data2

pl " Results, ansifilter with a color, bbcode color:"
tail -n+2 data2 |
./p1 > f1
color defaulters f1 > f2
ansifilter --bbcode f2 > f3
cat f3

producing:

.----------------------------------------.
| parameter_name  | status     | comment |
+-----------------+------------+---------+
| banking         | ok         | NA      |
| Loan_department | ok         |     20% |
| HR_Group        | defaulters | Ajay    |
'-----------------+------------+---------'

cheers, drl