LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-13-2014, 07:31 AM   #76
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387

Have a look at this:
Code:
opt1_AAD=$( awk -F"[:,]" '/^[0-9]/{ X=$1*3600+$2*60+$3 }END { print X }' opt1.txt )
Use fields 4, 5 and 6 for opt1_ASA

Last edited by druuna; 01-13-2014 at 07:33 AM.
 
1 members found this post helpful.
Old 01-14-2014, 01:00 AM   #77
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Thanks a lot Friend :-) . It's really working fine.

now if i have seconds 624 i want to convert that to HH:MM:SS using awk how could we do that for first field in file opt4.txt

opt4.txt:
"OldestCallWaiting"||','||"AverageSpeedOfAnswer"||','||"CallsWaiting"||','||"CallsAnswered"||','||"C allsAbandoned"
------------------------------------------------------------------------------------------------------------------
624,372,27,82,10


opt4_AAD=First Field(ie. 624) from opt4.txt i want to convert it to HH:MM:SS using awk

could you please help me with this?

Last edited by santosh0782; 01-14-2014 at 01:01 AM.
 
Old 01-14-2014, 03:28 AM   #78
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
The solution is similar to the previous one:

- set the correct field separator(s): A , (comma) would suffice this time,
- look for a line that start with a number (the /^[0-9]/ part),
- do the arithmetic with $1 (624 in the above example).

There is one catch compared with the bash solution from post #68: Awk uses floating point numbers and bash does not, which means that you need to use printf to use decimal integers. Something like this, where h, m and s hold the calculated value: printf("%02d:%02d:%02d\n", h, m, s)

Give it a try with the above info. I'm sure you'll report back if you run into something


BTW: My previous example can be simplified to this:
Code:
awk -F"[:,]" '/^[0-9]/{ X=$1*3600+$2*60+$3 ; print X }'
The END{ } part isn't needed, the print statement can be part of the main body.
 
Old 01-14-2014, 04:24 AM   #79
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
I tried this way:
awk -F ',' '/^[0-9]/ { ((h=${1}/3600));m=(${1}%3600)/60));((s=${1}%60)) print "%02d:%02d:%02d\n" $h $m $s }' opt4.txt

Error:
awk: /^[0-9]/ { ((h=${1}/3600));m=(${1}%3600)/60));((s=${1}%60)) print "%02d:%02d:%02d\n" $h $m $s }
awk: ^ syntax error

need your help to calculate the first field in opt4.txt
 
Old 01-14-2014, 04:31 AM   #80
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by santosh0782 View Post
I tried this way:
Code:
awk -F ',' '/^[0-9]/ { ((h=${1}/3600));m=(${1}%3600)/60));((s=${1}%60)) print "%02d:%02d:%02d\n" $h $m $s }' opt4.txt
need your help to calculate the first field in opt4.txt
- The double round brackets are bash specific (this: ((h=${1}/3600)) ).
- In awk you do not use curly brackets around the field number: ${1} --> $1

Code:
awk -F"," '/^[0-9]/{ h=$1/3600 ; m=($1%3600)/60 ; s=$1%60 ; printf("%02d:%02d:%02d\n", h, m, s) }' opt4.txt
 
1 members found this post helpful.
Old 01-14-2014, 05:52 AM   #81
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Thanks a ton :-) It's working fine..

Appreciated your help :-)
 
Old 01-14-2014, 05:54 AM   #82
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Hi Friend,

do you have any link where i can study to write functions and call them in a script.

I want to study Functions in details and also awk in detail, if you have any links or your notes could you please provide me the same?
 
Old 01-14-2014, 06:17 AM   #83
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by santosh0782 View Post
do you have any link where i can study to write functions and call them in a script.

I want to study Functions in details and also awk in detail, if you have any links or your notes could you please provide me the same?
General references for Bash, Awk etc can be found here: Resources / References / Useful links (one of my blog entries here at LQ).

Bash functions specific:
- BASH Programming - Introduction HOW-TO --> Chapter 8
- Bash Guide for Beginners --> Chapter 11
- Advanced Bash-Scripting Guide --> Multiple chapters/entries
 
1 members found this post helpful.
Old 01-15-2014, 11:47 PM   #84
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
ok. Thanks a ton Friend :-)

I'll go through those links, if need any help will catch you again :-)
 
Old 01-16-2014, 04:43 AM   #85
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Hi Friend,

piece of code from existing script:
Code:
#Convert to epcoh

set -- $(date -u '+%Y %j %H %M %S');DAYS=$(( 365*($1 - 1970) + ($1 - 1969)/4 + $2 -1 ));UT=$(( $5 + 60*($4 + 60*($3 + 24*$DAYS)) ))

#last 24 hours

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
do
./convert_dt.pl $UT >> ./pay_dt1.txt
./convert_dt1.pl $UT >> ./pay_dt2.txt
UT=`expr $UT - 3600`
done
code for convert_dt.pl:
Code:
#!/usr/bin/perl

$num_args = $#ARGV + 1;
die "Usage: this-program epochtime (something like '1219822177')" if
($num_args != 1);

$epoch_time = $ARGV[0];

($sec,$min,$hour,$day,$month,$year) = localtime($epoch_time);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

printf "%02d-%02d-%02d %02d\n", $year, $month, $day, $hour;
code for convert_dt1.pl:
Code:
#!/usr/bin/perl

$num_args = $#ARGV + 1;
die "Usage: this-program epochtime (something like '1219822177')" if
($num_args != 1);

$epoch_time = $ARGV[0];

($sec,$min,$hour,$day,$month,$year) = localtime($epoch_time);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

printf "%02d%02d%02d%02d\n", $year, $month, $day, $hour;

there is some problem with this script,

requirement is we need last 24 hours date and time into the pay_dt1.txt and pay_dt2.txt.

Pay_dt1.txt is having the data:
2014-01-14 09
2014-01-14 08
2014-01-14 07
2014-01-14 06
2014-01-14 05
2014-01-14 04
2014-01-14 03
2014-01-14 02
2014-01-14 01
2014-01-14 00
2014-01-13 23
2014-01-13 22
2014-01-13 21
2014-01-13 20
2014-01-13 19
2014-01-13 18
2014-01-13 17
2014-01-13 16
2014-01-13 15
2014-01-13 14
2014-01-13 13
2014-01-13 12
2014-01-13 11
2014-01-13 10


and pay_dt2.txt is having this data:
2014011409
2014011408
2014011407
2014011406
2014011405
2014011404
2014011403
2014011402
2014011401
2014011400
2014011323
2014011322
2014011321
2014011320
2014011319
2014011318
2014011317
2014011316
2014011315
2014011314
2014011313
2014011312
2014011311
2014011310


Todays date is 20140116 but it is showing the 13th and 14th data, how could we make the above script to just put the last 24hours date and time?

Last edited by santosh0782; 01-16-2014 at 04:48 AM.
 
Old 01-16-2014, 05:49 AM   #86
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by santosh0782 View Post
piece of code from existing script:
Code:
#Convert to epcoh

set -- $(date -u '+%Y %j %H %M %S');DAYS=$(( 365*($1 - 1970) + ($1 - 1969)/4 + $2 -1 ));UT=$(( $5 + 60*($4 + 60*($3 + 24*$DAYS)) ))

#last 24 hours

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
do
./convert_dt.pl $UT >> ./pay_dt1.txt
./convert_dt1.pl $UT >> ./pay_dt2.txt
UT=`expr $UT - 3600`
done
I don't believe the output from the bold part is correct and if I interpreted it correctly it can be done simpler. I assume that the bold part is suppose to give you the amount of days and seconds from epoch, which can be done like this:
Code:
UT=$(date --utc +%s)     # seconds from epoch
DAYS=$(( $UT / 86400 ))  # days from epoch
There is a 2 day difference between your command (DAYS -> 16084) and mine (DAYS -> 16086) and the seconds are also not the same. Mine is correct (have a look here: epoch converter).

Both perl scripts also seem to be overkill. The only thing they do is convert seconds from epoch to a specific date format, which the date command can do for you:
Code:
$ date --date @1389867412 +'%Y-%m-%d %H'  # output convert_dt.pl produces
2014-01-16 11

$ date --date @1389867412 +'%Y%m%d%H'     # output convert_dt1.pl produces
2014011611

# using a variable as input:
$ UT=1389867412
$ date --date @$UT +'%Y%m%d%H'
2014011611
All in all it seems you are trying to do this the hard way. But you, again, did not provide any details on the overall goal so I cannot help you with any specifics. It does look like an earlier question you asked, you might want to revisit post #59 which is an answer to post #54
 
Old 01-16-2014, 11:17 AM   #87
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Actually this is a huge script,i do not understand it completely, however i got the work to correct only the below part of code:
Code:
#Convert to epcoh

set -- $(date -u '+%Y %j %H %M %S');DAYS=$(( 365*($1 - 1970) + ($1 - 1969)/4 + $2 -1 ));UT=$(( $5 + 60*($4 + 60*($3 + 24*$DAYS)) ))

#last 24 hours

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
do
./convert_dt.pl $UT >> ./pay_dt1.txt
./convert_dt1.pl $UT >> ./pay_dt2.txt
UT=`expr $UT - 3600`
done
Requirement is we need Last 24 hours date and time in the two files pay_dt1.txt and pay_dt2.txt

the above script should provide the last 24hours date and time in this file Pay_dt1.txt with the format:
2014-01-14 09
2014-01-14 08
2014-01-14 07
2014-01-14 06
2014-01-14 05
2014-01-14 04
2014-01-14 03
2014-01-14 02
2014-01-14 01
2014-01-14 00
2014-01-13 23
2014-01-13 22
2014-01-13 21
2014-01-13 20
2014-01-13 19
2014-01-13 18
2014-01-13 17
2014-01-13 16
2014-01-13 15
2014-01-13 14
2014-01-13 13
2014-01-13 12
2014-01-13 11
2014-01-13 10

and in the file pay_dt2.txt, the above script should provide the last 24 hours date and time in the format:
2014011409
2014011408
2014011407
2014011406
2014011405
2014011404
2014011403
2014011402
2014011401
2014011400
2014011323
2014011322
2014011321
2014011320
2014011319
2014011318
2014011317
2014011316
2014011315
2014011314
2014011313
2014011312
2014011311
2014011310



with these dates, they are checking what data is available for last 24 hours and doing further calculations and displaying it.

the code you provided is just giving the current date and time:
Code:
date --date @$UT +'%Y%m%d%H'
code for convert_dt.pl:

Code:
#!/usr/bin/perl

$num_args = $#ARGV + 1;
die "Usage: this-program epochtime (something like '1219822177')" if
($num_args != 1);

$epoch_time = $ARGV[0];

($sec,$min,$hour,$day,$month,$year) = localtime($epoch_time);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

printf "%02d-%02d-%02d %02d\n", $year, $month, $day, $hour;
code for convert_dt1.pl:
Code:
#!/usr/bin/perl

$num_args = $#ARGV + 1;
die "Usage: this-program epochtime (something like '1219822177')" if
($num_args != 1);

$epoch_time = $ARGV[0];

($sec,$min,$hour,$day,$month,$year) = localtime($epoch_time);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

printf "%02d%02d%02d%02d\n", $year, $month, $day, $hour;

could you please help me to get last 24hours time in that two files?
 
Old 01-16-2014, 11:44 AM   #88
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Code:
#Convert to epcoh
UT=$(date --utc +%s)
DAYS=$(( $UT / 86400 ))

#last 24 hours
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
do
  date --date @$UT +'%Y-%m-%d %H' >> pay_dt1.txt
  date --date @$UT +'%Y%m%d%H'    >> pay_dt2.txt
  (( UT-=3600 ))
done
I'm a bit disappointed, my previous post told you everything you needed to know.....
 
1 members found this post helpful.
Old 01-16-2014, 12:05 PM   #89
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
I am really sorry, Friend, actually i am working on multiple scripts simultaneously and as i am a new to this scripting world, its taking little time to understand all the things, but seniors are putting pressure to complete the work as early as i can, hence i was not able to catch the previous post.

But i really want to be like you, i am putting my efforts to learn the things quickly.. but would need your guidance to be an expert in scripting.

Thanks a ton, for you help :-)
will keep you asking the queries..
 
Old 01-16-2014, 12:56 PM   #90
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by santosh0782 View Post
I am really sorry, Friend, actually i am working on multiple scripts simultaneously and as i am a new to this scripting world, its taking little time to understand all the things, but seniors are putting pressure to complete the work as early as i can, hence i was not able to catch the previous post.
So in essence I'm doing your work because you won't tell your seniors that your workload is high enough as it is....... That's something I do not appreciate.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error running cron job, syntax error? mpyusko Debian 2 12-20-2012 10:57 AM
script.pl with sed shell calls: sh error syntax error near unexpected token `(' MMaddoxx Programming 7 11-24-2011 09:00 AM
syntax error on running configure file in cygwin sugan General 1 10-08-2010 02:34 AM
why am getting error ksh: syntax error: `fi' unexpected while running script deb4you Linux - Newbie 4 09-06-2008 09:37 AM
ERROR running make when installing Nvidia drivers (syntax error) randyriver10 Linux - Software 5 02-21-2004 05:51 PM


All times are GMT -5. The time now is 03:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration