Output in table format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output in table format
# 8  
Old 04-18-2014
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:
Code:
#!/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:
Code:
$ ./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:
Code:
...
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:
Code:
.----------------------------------------.
| parameter_name  | status     | comment |
+-----------------+------------+---------+
| banking         | ok         | NA      |
| Loan_department | ok         |     20% |
| HR_Group        | defaulters | Ajay    |
'-----------------+------------+---------'

cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell output format like table

Hi, OS: Redhat 7.5 shell: Bash Wrote below script to login into oracle via shell script and trying to reset locked account..It works as expected. But I need specific output << EOF should go to target terminal not all out put running below script from ansible command line.. #!/bin/bash... (1 Reply)
Discussion started by: onenessboy
1 Replies

2. Shell Programming and Scripting

How to create a file from output of vertica table query in UTF-8 format?

Hello, In my shell script, I extract table data from HP Vertica DB into a csv file using vsql -c command. But the problem is the file getting created is in binary format and hence some of the data becomes unreadable which has chinese characters as part of data. file -i filename.csv - gives... (2 Replies)
Discussion started by: Dharmatheja
2 Replies

3. Shell Programming and Scripting

Arranging the command output into an html table format

Hi, I need to format a command output for the beolow command: runmqckm -cert -list -db $MQ_KDB -pw $PASSWD -expiry $EXP | grep -v "Certificates in database" The output will be: "ABC - cert name" From: Tuesday, May 25, 1999 11:09:40 AM CDT To: Saturday, May 25, 2019 11:39:40 AM CDT ... (3 Replies)
Discussion started by: bdpl
3 Replies

4. Shell Programming and Scripting

Need the output in the mentioned Table format

Hi Friends, I have the script output like below: Script Output: ----------------------------------------------------------------------- Details of the Client: ----------------------- name: server1; save set: All; ... (3 Replies)
Discussion started by: akmani
3 Replies

5. Shell Programming and Scripting

I want query output to send on mail using table tag and output should be in table

#! /bin/ksh #] && . ./.profile 2>/dev/null if test -f '.profile'; then . ./.profile; fi; #. .profile LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1"; pzTitle="$2"; pzMsg="$3"; pzFrom="$4"; pzTo="$5"; pzFiles="$6"; pzReplyTo="$7" ( ... (21 Replies)
Discussion started by: ankit.mca.aaidu
21 Replies

6. Shell Programming and Scripting

Normal text to table format

Hi, I am trying to show my list, from a simple list format to a table (row and column formatted table) Currently i have this format in my output (the formart it will always be like this ) >> first 3 lines must be on the same line aligned, and the next 3 shud be on 2nd line....: INT1:... (10 Replies)
Discussion started by: eboye
10 Replies

7. Shell Programming and Scripting

convert the output in table format

Hi All, I have a output like below values val1=test.com val2=10.26.208.11 val3=en1 val4=test-priv1.com val5=192.168.3.4 val6=en2 val7=test-priv2.com val8=192.168.4.4 val9=en3 val10=test-vip.com val11=10.26.208.9 val12=$val3 I want to convet this output values into below... (1 Reply)
Discussion started by: kamauv234
1 Replies

8. Shell Programming and Scripting

Help with perl script to output data in table format...

Hello, I need help with a perl script that will process a text file and match virtual server name to profile(s). the rest will be ignored. Virtual server name follows the word "virtual" in the begging of the line. There could be multiple profiles assigned to one virtual server. For example, ... (3 Replies)
Discussion started by: besogon
3 Replies

9. Shell Programming and Scripting

Table format

My Code Hi Friends, I need to get output in table format using unix shell script.For example my server CPU and memory utilization report will come as a mail with ordinary format but i want to make as table format. Here two output finally we are getting:- CPU utilization is... (2 Replies)
Discussion started by: susindram
2 Replies

10. Programming

Create table with date format.

Hello, Could you please let me know the correct format of CREATE TABLE statement with DATE format. CREATE TABLE EMP_TABLE1 ( NAME VARCHAR(6) PRIMARY KEY, ADDRESS VARCHAR(6), BIRTH_DATE DATE ); I want BIRTH_DATE to be in "YYYYMMDDHHMISS" format. How we can create table with... (4 Replies)
Discussion started by: Poonamol
4 Replies
Login or Register to Ask a Question