displaying output in a table.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting displaying output in a table.
# 1  
Old 10-06-2006
displaying output in a table.

Hello,

I've just finished my first script (about displaying open ports on the computer and who opened them) and everything is fine with it but I want to style it a little bit just for the sake of learning how to do this.
What I want to do is make the display like the one of ps, for example, i.e make it look like a table:

Code:
port         process              files
port1        pr1                    #of files   
port2

I've tried setting the port process and files with awk '{print "process" "port"}' and manipulating the FS but it is way not I wanted to achieve. Could anyone outline me the process of creating a table? Or even redirect me to some good reference on the web because I couldn't find one except for the Gawk variables but I can't figure it out from there.
Here is my source in case it would be easier for you this way:
Code:
#!/bin/bash
echo
echo This script will find all the open ports on a computer.
echo It will also show the process that has opened each one and the number of files opened per process.
echo
echo

showProcesses()
{
	netstat -atuv | awk '{print $4}' > ports.list
	lsof -i < ports.list | awk 'NR>1{print $1}'
}

showNumberOfFiles()
{
	lsof -i < ports.list | awk 'NR>1{print $2}' > files.list
	while read pid; do
	lsof -p $pid | awk '{print $1}' > numberoffiles.list
	wc -l < numberoffiles.list
	done < files.list
}

echo Open ports on your computer:
echo

netstat -tuv | awk 'NR>2{print $4}'

echo
echo
echo Processes on each port:
echo

showProcesses

echo
echo
echo Number of files per process:
echo

showNumberOfFiles


Last edited by rbatte1; 07-07-2016 at 06:37 AM.. Reason: Code tags to preserve multiple spaces
# 2  
Old 10-06-2006
Try using tabs instead of spaces, to align the columns.
# 3  
Old 10-06-2006
I've tried using awk'{print "port" "process" "files"}' but it is looped like this forever and even if I manage to do this afterwards I can't place the rest of the output on one line.
Is there a way for example of creating a table with awk by specifiying what goes in the fist column, what goes in the second, etc. I've read about the $1, $2, but as far as I understood this works only for formatting the output of a command not for inputting text into columns 1,2, etc.
# 4  
Old 10-06-2006
If you have used the printf formating in C, you can use the same this here for column alignments.


Code:
$ echo "Hai" | nawk '{
> printf("==========================================\n");
> printf("|%10s|%10s|%10s|%10s|\n","11","12","13","14");
> printf("|%10s|%10s|%10s|%10s|\n","21","22","23","24");
> printf("==========================================\n");
> }'

Code:
==========================================
|        11|        12|        13|        14|
|        21|        22|        23|        24|
==========================================


Last edited by rbatte1; 07-07-2016 at 06:37 AM.. Reason: Code tags
# 5  
Old 10-06-2006
mahendramahendr,
use vBcodes for proper post formating.
# 6  
Old 10-06-2006
Thanks mahendramahendr, I will try this when I'm at home. I found a very simple way of doing this with a program called paste- just paste file1 file2 file3 and it makes them as a table. It's great but screws up a little bit just in one case: when there is more than one process at a port. Then the allignment is not that good, but I can live with it Smilie.
Though, if anyone knows whether this can be fixed with paste only or has a different approach in mind, I would appreciate it.

Kindest regards,
Valentin
# 7  
Old 10-06-2006
Thanks vgersh99... This stuff is excellent..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Web Development

PHP table isn't displaying

I have a PHP script which connects to a MySQL database and should output a table in HTML. I have tired to set up a long-polling AJAX script to poll my PHP script every second. It seems to work based on what I can see in my browser debugger, however the table isn't showing on the page. Can anybody... (3 Replies)
Discussion started by: hadinetcat
3 Replies

3. Shell Programming and Scripting

Displaying log file pattern output in tabular form output

Hi All, I have result log file which looks like this (below): from the content need to consolidate the result and put it in tabular form 1). Intercomponents Checking Passed: All Server are passed. ====================================================================== 2). OS version Checking... (9 Replies)
Discussion started by: Optimus81
9 Replies

4. Homework & Coursework Questions

Displaying text from a MySQL table which can be modified

Hi am creating a website for my third year at uni, am trying to create a website where the client can update the content of the site themselves, i will have a news page and i want the content to be draw from my database and displayed on the front end of the site using php, i also want to have an... (1 Reply)
Discussion started by: richeyrich86
1 Replies

5. UNIX for Dummies Questions & Answers

Displaying text from a MySQL table which can be modified

Hi am creating a website for my third year at uni, am trying to create a website where the client can update the content of the site themselves, i will have a news page and i want the content to be draw from my database and displayed on the front end of the site i also want to have an admin side... (3 Replies)
Discussion started by: richeyrich86
3 Replies

6. Shell Programming and Scripting

Displaying the output in the tabular Format

Hi, I have a file which contains the data in the below format and need to develop a script which will give the output in the tabular format. Could you please advice me. Folder: Workflow: version . Workflow run status: Workflow run error code: Schedule time: Workflow run type: ... (2 Replies)
Discussion started by: kandi.reddy
2 Replies

7. Shell Programming and Scripting

Output Parameter value not displaying

Hi all, Below is my shell script that calls a stored procedure(with output parameter) I have tried executing this script but it doesn't display the output parameter value. Can anyone help me ? :( #!/bin/bash ###############################################################################... (7 Replies)
Discussion started by: saviochacko
7 Replies

8. Shell Programming and Scripting

script not displaying output correctly

Hi, I am having an issue with my script, ofcourse... I am trying to run commands against a remote server, I am pulling the hostnames or IPs from a file list, then looping thru and running the date cmd. I will be running different cmds just trying to get it working first. The ouput isn't... (2 Replies)
Discussion started by: dfezz1
2 Replies

9. Shell Programming and Scripting

Displaying Output in Columns

I'm writing a script to analyze the logs of an smtp relay machine and I'd like the final output to be displayed in columns showing results from the previous day, week, month, and 45 days. The problem I'm running into is that I can't figure out how to display the columns neatly so there is no... (1 Reply)
Discussion started by: jjamd64
1 Replies

10. Shell Programming and Scripting

Displaying output from df -k in GB instead of KB

Hello all, Code below: echo "Oracle Filesystems" echo "------------------" echo for j in `df -l -k |grep total|grep ora|grep -v storage|grep -v vg00|awk '{print $1}'` do echo $j is `df -l -k $j |grep total|grep ora|grep -v storage|grep -v vg00|awk -F":" '{print $2}'|awk '{print $1}'` KB... (6 Replies)
Discussion started by: LinuxRacr
6 Replies
Login or Register to Ask a Question