Create a report for client with a text data file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a report for client with a text data file
# 1  
Old 03-30-2012
Create a report for client with a text data file

Hi,
I am an amateur bash scriptwriter and I need to write a script which creates a report in a formatted, easy to read table-like that is displayed to standard output. The script has to export the followings: Process ID,User Name, Command Name,Priority.....

Now I have a file that I can see all the report as a text, separated by comma like this:

1,root,init,20,0.0,0.1,0:01.78
1008,root,migration/0,1,2.0,1.8,7:04.32
5,root,watchdog/0,1,0.0,0.0,0:00.00

I need to have (-u option) which gives me a user specified by regex like this: myscript -u mytxt.top
This has to give me: The user name,The process name,The process ID,% of CPU Time,% of Memory
The output has to be like this:

Image

I know that I have to use AWK or SED to get the total of CPU and other stuff... however I am not that comfortable to write this...
any help would be appreciated.
Thanks

---------- Post updated at 11:54 PM ---------- Previous update was at 09:05 PM ----------

great! no solution?!
# 2  
Old 03-30-2012
Hi.

If you think that this section is taking too long to get an answer, you may want to consider posting in another section:
Quote:
Emergency UNIX and Linux Support !! Help Me!!
Post your urgent questions here for highest visibility. Posting a new thread to this forum requires Bits. We monitor this forum to help people with emergencies, but we do not guarantee response time or answers.
Here is a script that should get you started:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate extraction and formatting data into table.

# Display local environment.
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() { : ; }
pe
C=$HOME/bin/context && [ -f $C ] && $C awk tbl nroff

NAME=${1-root}

# Copy head tbl boilerplate.
cat <<EOF > f1
.TS
box;
|l|l|r|r|r|
-----
|l|l|r|r|r|.
USER	PROCESS	PROC ID	% CPU	% MEM
EOF

# Extract data from csv to tsv.
awk -F, -v user="$NAME" '
BEGIN	{ OFS = "\t" }
$2 ~ user	{ print $2,$3,$1,$5,$6 }
' data0 >> f1

# Copy tail boilerplate
echo ".TE" >> f1

# Format, replace nroff as necessary.
pe
tbl f1 |
nroff

exit 0

producing:
Code:
$ ./s1 

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
awk GNU Awk 3.1.5
tbl GNU tbl (groff) version 1.18.1
nroff GNU nroff (groff) version 1.18.1

+-----+-------------+---------+-------+-------+
|USER | PROCESS     | PROC ID | % CPU | % MEM |
+-----+-------------+---------+-------+-------+
|root | init        |       1 |   0.0 |   0.1 |
|root | migration/0 |    1008 |   2.0 |   1.8 |
|root | watchdog/0  |       5 |   0.0 |   0.0 |
+-----+-------------+---------+-------+-------+

( ... )

and for:
Code:
$ ./s1 boot

( ... )

+-----+---------+---------+-------+-------+
|USER | PROCESS | PROC ID | % CPU | % MEM |
+-----+---------+---------+-------+-------+
|boot | read0   |       9 |   4.4 |   5.5 |
+-----+---------+---------+-------+-------+

( ... )

With this data file:
Code:
1,root,init,20,0.0,0.1,0:01.78
1008,root,migration/0,1,2.0,1.8,7:04.32
5,root,watchdog/0,1,0.0,0.0,0:00.00
9,boot,read0,1,4.4,5.5,0:00.00

See man pages for details, and experiment.

Best wishes ... cheers, drl

( Edit: minor change in awk to look at field 2, not entire line. )

Last edited by drl; 03-30-2012 at 01:16 AM..
These 2 Users Gave Thanks to drl For This Post:
# 3  
Old 03-30-2012
it is very complicated for me as I am a newbie! I did some research and I found out that I can use "getopts" to work around this. what do u think?

---------- Post updated at 01:13 AM ---------- Previous update was at 12:31 AM ----------

I am sure that getopts is very helpful here. This is the idea:
Code:
while getopts ":c:u:" opt; do
  case $opt in
    c)    echo "-c was triggered, Parameter: $OPTARG" >&2;;
    u)    echo "-u was triggered, Parameter: $OPTARG" >&2;;
    \?)    echo "Invalid option: -$OPTARG" >&2;exit 1;;
    *)    echo "Option -$OPTARG requires an argument." >&2;exit 1;;
  esac
done

# 4  
Old 03-30-2012
Hi.
Quote:
Originally Posted by bashily
it is very complicated for me as I am a newbie! I did some research and I found out that I can use "getopts" to work around this
I am not sure what it is that you are working around, however, yes, getopts is a good way to standardize the interface for scripts, and to ease the task of maintenance.

Best wishes ... cheers, drl

---------- Post updated at 10:08 ---------- Previous update was at 06:47 ----------

Hi.

Quote:
I take a course for scripting and this is what I need to do for the final project. ... I am not able to figure out this one, even though I am good at my stuff. I appreciate it if you help with this:

-- excerpt from private message
I sympathize with your situation and thank you for your honesty, however, you should be using the forum designed for classwork:
Coursework, etc., forum
Code:
 Homework & Coursework Questions  (4 Viewing)
Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

https://www.unix.com/homework-coursew...ons-forum.html

I do not plan to continue posting in this thread.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

2. Solaris

Create file for group of data:

Hi folks, I have the following data.Any help is greatly appreciated. order File_name 7222245 7222245.pdf 7222245 7222245a.pdf 7222245 7222245b.pdf 7222245 7222245c.pdf 7222245 7222245d.pdf 7222250 ... (1 Reply)
Discussion started by: kumar444
1 Replies

3. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

4. Shell Programming and Scripting

Need help in writing a script to create a new text file with specific data from existing two files

Hi, I have two text files. Need to create a third text file extracting specific data from first two existing files.. Text File 1: Format contains: SQL*Loader: Release 10.2.0.1.0 - Production on Wed Aug 4 21:06:34 2010 some text ............so on...and somwhere text like: Record 1:... (1 Reply)
Discussion started by: shashi143ibm
1 Replies

5. Shell Programming and Scripting

Find and replace data in text file with data in same file

OK I will do my best to explain what I need help with. I am trying to format an ldif file so I can import it into Oracle oid. I need the file to look like this example. Keep in mind there are 3000 of these in the file. changetype: modify replace: userpassword dn:... (0 Replies)
Discussion started by: timothyha22
0 Replies

6. Shell Programming and Scripting

Create multiple text file from a single text file on AIX

Hi I need to create multiple text files from onc text file on AIX. The data of text files is as below: ********************************************** ********************************************** DBVERIFY: Release 10.2.0.4.0 - Production on Tue Nov 10 13:45:42 2009 Copyright (c) 1982,... (11 Replies)
Discussion started by: lodhi1978
11 Replies

7. Shell Programming and Scripting

How to format or create a matrix report from file

Dear Unix champs, I have a input file as attached, i would like to create an report from the file as below FileType | EQUENS0001 | EQUENS0002 | EQUENS1100 | EQUENS0003 --------+-------------------------------------------------------- Msg No |... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

8. UNIX for Dummies Questions & Answers

create data file from report file

Dear Ones, kindly help me to create a data file from the report file as shown here under( i am new one to unix KNOW BASIC COMMANDS) file:rama.prt (ist record)(3 to 4 lines of text with different filed names) Name :M.LALITHA DOB:12/11/45 DESG :JA(P) STANO:300175 ... (3 Replies)
Discussion started by: cvvsnm
3 Replies
Login or Register to Ask a Question