Help with Display Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Display Shell Script
# 1  
Old 05-13-2012
Help with Display Shell Script

hi Friends,
I am writing a shell script to extract data from sar logs for my daily analysis.
I have this in sar logs
Code:
HP-UX dhpcdbe1 B.11.31 U ia64    05/09/12

01:22:42   device   %busy   avque   r+w/s  blks/s  avwait  avserv
01:27:59   disk30    8.79   13.72      26     427   36.47   10.09
           disk50   10.48   12.87      29     449   36.78   10.74
          disk745    0.00    0.50       0       0    0.01    0.41
          disk746    0.05    0.50       0       0    0.00    8.33
          disk747    0.03    0.50       0       0    0.01    6.00
          disk748    0.07    0.50       0       0    0.00   11.09

What i want is something like below:

Code:
01:22:42   device   %busy   avque   r+w/s  blks/s  avwait  avserv
01:27:59   disk30    8.79   13.72      26     427   36.47   10.09
01:27:59   disk50   10.48   12.87      29     449   36.78   10.74
01:27:59   disk745    0.00    0.50       0       0    0.01    0.41
01:27:59   disk746    0.05    0.50       0       0    0.00    8.33
01:27:59   disk747    0.03    0.50       0       0    0.01    6.00
01:27:59   disk748    0.07    0.50       0       0    0.00   11.09

So what i am trying to do is fill those rows which dont have the timings with the immediate previous one which has, like here the second row.

What challenges i am facing:
1. What logic to implement and how to get the timings in all the rows filled?
Its been a long time since i have written a script .
2. Something i tried was

Code:
#!/usr/bin/ksh
TIMESTAMP_SAR=`date +%d%m%Y_%H%M%S`
tt=$1
awk 1 $tt > $TIMESTAMP_SAR
perl -pi -e '$_ = "" if ($. <= 3);' $TIMESTAMP_SAR

cut -d"d" -f2- $TIMESTAMP_SAR |perl -pi -e 's/^/d/g' > ${TIMESTAMP_SAR}_3
awk -F"d" '{print $1}' $TIMESTAMP_SAR > ${TIMESTAMP_SAR}_4
perl -pi -e 's/ //g' ${TIMESTAMP_SAR}_4
awk -F\t '{if($1=="")$1=string}1' OFS="\t" string=0 ${TIMESTAMP_SAR}_4 > ${TIMESTAMP_SAR}_5
#below part fills the times for missing rows same as row before
######
c=1
while read line
do
a=${#line}
b=${line}
if [[ "$a" -eq "$c" ]]
then
b=${d}
echo $b
else
echo $b
d=${b}
fi
done < ${TIMESTAMP_SAR}_5 > ${TIMESTAMP_SAR}_6
######
paste ${TIMESTAMP_SAR}_6 ${TIMESTAMP_SAR}_3 > ${TIMESTAMP_SAR}_7

But the problem with this logic is if the device column has any name starting with any other alphabet other than 'd' then it doesn't work.

Why i am doing all this?
Basically i will convert all the sar data into csv format and put it in a db table and then make graphs using excel ,by connecting to the db , whenever i need to .

Please can you suggest the solutions to my challenges.

Thanks,
Kunwar
# 2  
Old 05-13-2012
try this..
Code:
perl -nle 'if($.>2) { if (/(^\d{2}:\d{2}:\d{2})/) { $time=$1;print $_;}else{s/^\s+/$time   /;print $_;}}' filename

# 3  
Old 05-13-2012
A try in awk :
Code:
awk '$1 ~ /[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/ { a=$1"\t" }  { print ( $1 != a ) ? a $0 : $0 }' input

# 4  
Old 05-13-2012
thanks you guys...It helped..Below is o/p:

Code:
> perl -nle 'if($.>2) { if (/(^\d{2}:\d{2}:\d{2})/) { $time=$1;print $_;}else{s/^\s+/$time   /;print $_;}}' tt3

01:22:42   device   %busy   avque   r+w/s  blks/s  avwait  avserv
01:27:59   disk30    8.79   13.72      26     427   36.47   10.09
01:27:59   disk50   10.48   12.87      29     449   36.78   10.74
01:27:59   disk745    0.00    0.50       0       0    0.01    0.41
01:27:59   disk746    0.05    0.50       0       0    0.00    8.33
01:27:59   disk747    0.03    0.50       0       0    0.01    6.00
01:27:59   disk748    0.07    0.50       0       0    0.00   11.09


> awk '$1 ~ /[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/ { a=$1"\t" }  { print ( $1 != a ) ? a $0 : $0 }'  tt3

HP-UX dhpcdbe1 B.11.31 U ia64    05/09/12

01:22:42        01:22:42   device   %busy   avque   r+w/s  blks/s  avwait  avserv
01:27:59        01:27:59   disk30    8.79   13.72      26     427   36.47   10.09
01:27:59                   disk50   10.48   12.87      29     449   36.78   10.74
01:27:59                  disk745    0.00    0.50       0       0    0.01    0.41
01:27:59                  disk746    0.05    0.50       0       0    0.00    8.33
01:27:59                  disk747    0.03    0.50       0       0    0.01    6.00
01:27:59                  disk748    0.07    0.50       0       0    0.00   11.09

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display calendar in correct format using shell script

Hi All, I'm trying to print calendar using shell script and i'm able to print it. But the format is not good. Here is the script. #!/bin/bash echo $(date) echo "Hello $USER" echo Hostname $(hostname) echo Working in $(pwd) echo Here is this month calender echo $(cal) $ sh first.sh... (7 Replies)
Discussion started by: chandrakanth
7 Replies

2. Shell Programming and Scripting

Help with Display Shell Script for date

hi friends, I am working on extracting data from sar logs for analysis. What is want to do is insert the date. The way i am doing is incrementing the date if i see the hour gets reduced (i.e. from 23 hours to 00 hours). Somehow the script which i have made is not able to handle the logic w.r.t... (3 Replies)
Discussion started by: kunwar
3 Replies

3. Shell Programming and Scripting

How to display a counter in shell script?

Hi, I am writing a script which processes large number of files in a directory. I wanto display a counter which increment after processing each file. I am processing each file in a for loop. If I echo a variable with its value increasing with each file, I will get around 5000 lines as output.... (10 Replies)
Discussion started by: jaiseaugustine
10 Replies

4. Shell Programming and Scripting

Display both html and plain text in email in shell script

Hi, I want to display both html and plain in email in my script. i tried the below code code: export MAILTO="ssi@a.com" export CONTENT1="$htmlfile" export CONTENT2="$plainfile" export SUBJECT="INFO " export MAILFROM="si@a.com" ( echo "Subject: $SUBJECT" echo "MIME-Version:... (4 Replies)
Discussion started by: sreelu
4 Replies

5. Shell Programming and Scripting

How to write a shell script to display files in single path?

Hello friends, I am a script which dispalys a multiple files with their contents. for exm: suppose two file test1.txt and test2.txt. when I run my script it have to display the below O/P. test1.txt -rw-r----- 1 sranga staff 91 Sep 23 02:18 calc.sh -rw-r----- 1 sranga ... (2 Replies)
Discussion started by: sivaranga001
2 Replies

6. UNIX Desktop Questions & Answers

Shell script to display user logged in within given time

how can i know which users have logged in at specified given start and end time in 24 hour format? (3 Replies)
Discussion started by: meherzad4u
3 Replies

7. Shell Programming and Scripting

Shell script to display top 5 process (per cpu consumption)

Hello, I have to create shell script being myself real novice in scripting... I was looking on the Internet for some script examples, but I don't have the basic understanding of the shell. I was trying to do something with “ps” command without success. For exemple total number of... (2 Replies)
Discussion started by: mr_awd
2 Replies

8. Shell Programming and Scripting

shell script that adds two integers and display answer on screen

please help shell script that adds two intergers and display answer on screen (1 Reply)
Discussion started by: wanyac2
1 Replies

9. Shell Programming and Scripting

shell script that would display installing.... when installign something

Hi, I am developing a shell script that would install a software when it is run. I want to displaying Installing <so and so>........ when it is actually installing it. I want it to be dynamic, in the sense, I want the dots after installing grow and drop. Is there a way we can do it.... (1 Reply)
Discussion started by: eamani_sun
1 Replies

10. UNIX for Dummies Questions & Answers

Shell Script Display?

I remember learning that there is a way to make a shell script display the script itself to standard output while the script is being executed but I can't find how to do that. Any pointers? (2 Replies)
Discussion started by: wmosley2
2 Replies
Login or Register to Ask a Question