Needs help on shell programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Needs help on shell programming
# 1  
Old 06-12-2009
Question Needs help on shell programming

Hi guys,

I need some help to create a script or command Smilie I do not have much experience in shell programming Smilie


I have a file with the lines below on 10000 lines:

12.06.09/14:20:13 4 sms/s
12.06.09/14:20:16 4 sms/s
12.06.09/14:20:17 3 sms/s
12.06.09/14:20:18 3 sms/s
12.06.09/14:20:25 5 sms/s
12.06.09/14:20:26 6 sms/s
12.06.09/14:20:29 2 sms/s
12.06.09/14:20:32 8 sms/s
12.06.09/14:20:33 4 sms/s
12.06.09/14:20:34 2 sms/s
12.06.09/14:20:35 3 sms/s


As you can see, I do not have a line for all seconds which make it really difficult to create graph.
So I try to create a line like 12.06.09/14:20:16 0 sms/s for the empty seconds.

For example :

12.06.09/14:20:13 4 sms/s
12.06.09/14:20:14 0
12.06.09/14:20:15 0
12.06.09/14:20:16 4 sms/s
12.06.09/14:20:17 3 sms/s
12.06.09/14:20:18 3 sms/s
12.06.09/14:20:19 0
12.06.09/14:20:20 0
12.06.09/14:20:21 0
12.06.09/14:20:22 0
12.06.09/14:20:23 0
12.06.09/14:20:24 0
12.06.09/14:20:25 5 sms/s
12.06.09/14:20:26 6 sms/s



That sounds easy but I can't get it to work and I feel like I'm not doing it the good way. Smilie

Does anybody knows how to make something like this ?


Thanks in advance for your help.
Julien
# 2  
Old 06-12-2009
What have you tried?
# 3  
Old 06-12-2009
Response

Hi;


I have writed the script below:

#!/bin/sh

#Definition du fichier

while read line
do
hour=`echo "$line" | awk '{print $1}' | cut -c 10-11`
minute=`echo "$line" | awk '{print $1}' | cut -c 13-14`
seconde=`echo "$line" | awk '{print $1}' | cut -c 16-17`
value=`echo "$line" | awk '{print $2,$3}`
echo "$hour:$minute:$seconde $value"

number=1
hour1=$(( hour1 +number ))
echo $hour1


done < "extract"


I guess that the best way to do it is to compare variable, so I cut the time value in hour, minute and seconde variable.

also I don't know how to manage the comparison if the past occurence= 0 and the next one >1 then echo ....
# 4  
Old 06-12-2009
Assuming that the dates in your file are sorted in increasing order, you could use Perl to densify your data as follows:

Code:
C:\>
C:\>type data.txt
12.06.09/14:20:13 4 sms/s
12.06.09/14:20:16 4 sms/s
12.06.09/14:20:17 3 sms/s
12.06.09/14:20:18 3 sms/s
12.06.09/14:20:25 5 sms/s
12.06.09/14:20:26 6 sms/s
12.06.09/14:20:29 2 sms/s
12.06.09/14:20:32 8 sms/s
12.06.09/14:20:33 4 sms/s
12.06.09/14:20:34 2 sms/s
12.06.09/14:20:35 3 sms/s
C:\>
C:\>type densify.pl
#!perl -w
use Date::Calc;
my $startdate;
my $enddate;
 
# process file, populate hash, set start and end dates
# IMPORTANT - it is assumed that the dates in the file are sorted
open(DATES,"data.txt") or die "Can't open data.txt: $!";
while (<DATES>) {
  chomp($line=$_);
  ($key = $line) =~ s/^([^ ]+) (.*)$/$1/;
  ($value = $line) =~ s/^([^ ]+) (.*)$/$2/;
  $startdate = $key if $. == 1;
  $enddate = $key;
  $exists{$key} = $value;
}
close(DATES) or die "Can't close data.txt: $!";
 
# split startdate and create @startts = (No_of_days_since_1/1/1, <additional_seconds>)
($sdy,$smn,$syr,$shr,$smi,$ssc) = split /[\.\/:]/, $startdate;
$syr += 2000;
@startts = ( Date::Calc::Date_to_Days($syr,$smn,$sdy), ($shr*60+$smi)*60+$ssc );
 
# split enddate and create @endts = (No_of_days_since_1/1/1, <additional_seconds>)
($edy,$emn,$eyr,$ehr,$emi,$esc) = split /[\.\/:]/, $enddate;
$eyr += 2000;
@endts = ( Date::Calc::Date_to_Days($eyr,$emn,$edy), ($ehr*60+$emi)*60+$esc );
 
# diff[0] = diff_of_days_from_common_epoch; diff[1] = diff_of_seconds
@diff = ($endts[0]-$startts[0], $endts[1]-$startts[1]);
 
# now loop through each second increment and print key value from hash if it exists, "0" otherwise
while ($diff[0] >= 0 and $diff[1] >= 0) {
  $key = sprintf("%02d.%02d.%02d/%02d:%02d:%02d",$sdy,$smn,substr($syr,2),$shr,$smi,$ssc);
  printf("%s %s\n",$key, defined $exists{$key} ? $exists{$key} : "0");
  ($syr,$smn,$sdy,$shr,$smi,$ssc) = Date::Calc::Add_Delta_YMDHMS($syr,$smn,$sdy,$shr,$smi,$ssc,0,0,0,0,0,1);
  @startts = ( Date::Calc::Date_to_Days($syr,$smn,$sdy), ($shr*60+$smi)*60+$ssc );
  @diff = ($endts[0]-$startts[0], $endts[1]-$startts[1]);
}
C:\>
C:\>
C:\>perl densify.pl
12.06.09/14:20:13 4 sms/s
12.06.09/14:20:14 0
12.06.09/14:20:15 0
12.06.09/14:20:16 4 sms/s
12.06.09/14:20:17 3 sms/s
12.06.09/14:20:18 3 sms/s
12.06.09/14:20:19 0
12.06.09/14:20:20 0
12.06.09/14:20:21 0
12.06.09/14:20:22 0
12.06.09/14:20:23 0
12.06.09/14:20:24 0
12.06.09/14:20:25 5 sms/s
12.06.09/14:20:26 6 sms/s
12.06.09/14:20:27 0
12.06.09/14:20:28 0
12.06.09/14:20:29 2 sms/s
12.06.09/14:20:30 0
12.06.09/14:20:31 0
12.06.09/14:20:32 8 sms/s
12.06.09/14:20:33 4 sms/s
12.06.09/14:20:34 2 sms/s
12.06.09/14:20:35 3 sms/s
C:\>
C:\>

Hope that helps,
tyler_durden
# 5  
Old 06-15-2009
perl script:

Code:
use strict;
my ($start,$prefix,$end);
while(<DATA>){
	if (/(.*:)([0-9]+).*/){
		if($. == 1){
			$start=$2;
			print $_;
		}
		else{
			$prefix=$1;
			$end=$2;
			for(my $i=$start+1;$i<$end;$i++){
				print $prefix,$i," 0\n";
			}
			print;
			$start=$end;
		}
	}
}
__DATA__
12.06.09/14:20:13 4 sms/s
12.06.09/14:20:16 4 sms/s
12.06.09/14:20:17 3 sms/s
12.06.09/14:20:18 3 sms/s
12.06.09/14:20:25 5 sms/s
12.06.09/14:20:26 6 sms/s
12.06.09/14:20:29 2 sms/s
12.06.09/14:20:32 8 sms/s
12.06.09/14:20:33 4 sms/s
12.06.09/14:20:34 2 sms/s
12.06.09/14:20:35 3 sms/s

# 6  
Old 06-15-2009
Wow

All right Guys,

thanks a lot for your help.
I really appreciate it.

I will ask one of my buddy here to help me to understand how that works.

Have a great day ! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Programming

Hi, I am using two files - one file contains list of service name , other file contains commands for each of these service name . I have to read each service name and check this string in 1.cfg file , if it exists , then i have to read another file (commands file ) and take the string and... (2 Replies)
Discussion started by: santhoshks
2 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

Shell programming

Hi every one,i am new to unix.Can any one tell me about shell programming.. (1 Reply)
Discussion started by: martina100011
1 Replies

4. Shell Programming and Scripting

SHELL PROGRAMMING

Using shell scripting, implement ‘scan.sh' that scans the file system recursively starting from current working directory and generates the file ‘index.txt' that contains a line for each file (or directory) with following fields in tab separated format: 1. The full path of the directory... (1 Reply)
Discussion started by: kranthikiran
1 Replies

5. Shell Programming and Scripting

Shell programming

Hi, Iam new to shell program, I want to check a file which is having same lines 2 times and i want to display it in a seperate file. File format is : AQWERTYU|1234567890 ASDFGHJK|0987654321 ZXCVBNML|1098576453 AQWERTYU|1234567890 I need to take the 1st and 4th lines in the above... (5 Replies)
Discussion started by: nivas
5 Replies

6. Shell Programming and Scripting

{} in shell programming

Could someone please tell me what {} mean when they surround a variable? For instance, $FILE = 'basename $1' //what is passed into this script $BANK = 'dirname $1' $INFILE = ${FILE}.${BANK}.$$ What does $INFILE contain after this assignment? Please let me know Thanks G (4 Replies)
Discussion started by: vgirijanaidu
4 Replies

7. Shell Programming and Scripting

shell programming

I want notes for learning Shell programming (2 Replies)
Discussion started by: Neha Agarwal
2 Replies

8. Shell Programming and Scripting

Shell Programming?

For what purposes should we use shell /what are the tasks we can achieve using shell which is best book to learn shell programming and will nayone tell me diff between shell programming aand shell scripting? Thank u in advance. (1 Reply)
Discussion started by: shrikrishna
1 Replies

9. UNIX for Dummies Questions & Answers

Shell Programming Help

Hi, I am new to shell programming, and hve been given a request to write a shell script. Is there any good places to go to see examples of how to write shell programming scripts? Thanks (4 Replies)
Discussion started by: mec585858
4 Replies

10. UNIX for Dummies Questions & Answers

Shell Programming

I have a fix_table.ksh script that takes a TABLENAME and a date. So, in jk_table_file.txt I have the tables...one per line, and in jk_out_file.txt I have the date in the format I need. The following doesn not 'want' to work in a shell script... for TABLE in `cat jk_table_file.txt`; do ... (2 Replies)
Discussion started by: JWK1
2 Replies
Login or Register to Ask a Question