Cut only time from string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cut only time from string
# 8  
Old 09-21-2015
Hello Khusbu,

Following may help you in same. Let's say we have 2 files named current_time and basic_time. Which are as follows.
Code:
cat current_time
04:23:16
  
cat basic_time
01:08:23

Then following is the code for same.
Code:
awk 'FNR==NR{split($0, A,":");current_time=A[1] * 3600 + A[2] * 60 + A[3];next} {split($0, B,":");time_from_logs=B[1] * 3600 + B[2] * 60 + B[3];if(time_from_logs > current_time){print "Current time is lesser than the logs time."} else {TOTAL=current_time - time_from_logs;printf "%02d:%02d:%02d\n", (TOTAL/3600), (TOTAL/60%60), (TOTAL%60)}}'  current_time basic_time

Output will be as follows.
Code:
03:14:53

You can change Input_file names as per your need here. Hope this helps you.

EDIT: Adding non one-liner form for solution here for same.
Code:
awk 'FNR==NR{
                split($0, A,":");
                current_time=A[1] * 3600 + A[2] * 60 + A[3];
                next
            }
            {
                split($0, B,":");
                time_from_logs=B[1] * 3600 + B[2] * 60 + B[3];
                if(time_from_logs > current_time){
                                                        print "Current time is lesser than the logs time."
                                                 }
                else                             {
                                                        TOTAL=current_time - time_from_logs;
                                                        printf "%02d:%02d:%02d\n", (TOTAL/3600), (TOTAL/60%60), (TOTAL%60)
                                                 }
            }
    '  current_time basic_time

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-21-2015 at 05:42 AM.. Reason: Added a non one-liner form of solution now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 09-21-2015
Convert the two time stamps to number of seconds from epoch and then subtract them. Once you have the difference, it's basic math to convert it back to a human readable string. If you have GNU date utility, there are options to convert time from many standard formats to number of seconds from epoch. Give it a try and let us know.
# 10  
Old 09-21-2015
Hi Ravinder,

Thanks for the code. Its working absolutely as we needed. But we want the 'TOTAL' variable to be declared globally. But as we are trying to print the TOTAL variable outside the awk command, it is not getting printed and hence we cannot reuse it. Please let me know how can we declare and use the variable outside the awk command.

Also, can we please get the time only in minutes? Since as per our new requirement, we need time to be stored only in minutes in TOTAL variable.

Thanks Smilie
# 11  
Old 09-21-2015
Hello Khushbu,

If I understood your requirement correctly then following may help you.
Code:
TOT=`awk 'FNR==NR{split($0, A,":");current_time=A[1] * 3600 + A[2] * 60 + A[3];next} {split($0, B,":");time_from_logs=B[1] * 3600 + B[2] * 60 + B[3];if(time_from_logs > current_time){print "Current time is lesser than the logs time."} else {TOTAL=current_time - time_from_logs;print TOTAL/60}}'  current_time basic_time`
  
echo "Here is the value of Total minutes: " $TOT

Output will be as follows.
Code:
Here is the value of Total minutes:  194.883

So you can use this variable any where else in your script, hope this helps.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 12  
Old 09-21-2015
Awesome Work Ravinder!! :-D

My colleague and me are really thankful to your constant efforts. And we really appreciate this. Finally we have completed with our scripting. Smilie Smilie

Thanks Again!!
Khushbu Smilie
# 13  
Old 09-21-2015
You are welcome Khushbu, I am glad I am able to help you Smilie. We all are here to learn/help/guide/advise each other, enjoy learning Smilie


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut string from shell

How can I cut the date and leave it in 3 variable? date="20181219" year=substr(date,1,4) monthsubstr(date,4,2) daysubstr(date,6,2) result year=2018 month=12 day=19 this does not work for me (3 Replies)
Discussion started by: tricampeon81
3 Replies

2. Shell Programming and Scripting

Cut the data with a string that has \ in it

Hi Team, Here's the record in a file. abc\USER DEFINED\123\345\adf\aq1 Delimiter here is "\USER DEFINED\" Expected output: abc|123\345\adf\aq1 Find "\USER DEFINED\" and replace it with "|". Can anyone please help us to fix this issue? Please use CODE tags as required by... (5 Replies)
Discussion started by: kmanivan82
5 Replies

3. Shell Programming and Scripting

Cut the string

---------- Post updated at 10:31 AM ---------- Previous update was at 10:28 AM ---------- Hello, I am trying to get the string cut based on the following needs: String1=Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44"... (6 Replies)
Discussion started by: cartrider
6 Replies

4. Shell Programming and Scripting

Cut a string for last 8 characters

Hello All I have a file like this abc.tpt.ctl bdc.tpt.ctl cdw.tpt.ctl I have looped every line using the for Loop, now I want to take each line and cut the .tpt.ctl part of it and store it in a variable and use the variable in same loop. The part I am stuck at is how do I cut the last... (9 Replies)
Discussion started by: nnani
9 Replies

5. Shell Programming and Scripting

Cut the string

Hi in a directory i've files having the following name for_category_info_19990101984301 for_catgry_meta_19991111214601 ------- I just want the name till year and month i.e; for_category_info_199901 for_catgry_meta_199911 How can i achieve the above string Thanks (2 Replies)
Discussion started by: smile689
2 Replies

6. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

7. Shell Programming and Scripting

how to cut all string after the last delimiter?

hi all, suppose a string: abc/def/ghi/jkl/mn.txt and i want to get the file name without the path. however, different files have different paths, therefore the number of delimiter is uncertain. thanks so much! (3 Replies)
Discussion started by: sunnydanniel
3 Replies

8. UNIX for Dummies Questions & Answers

'Cut'ting date time

My apache logs look like this... PHP Warning: Invalid argument supplied for foreach() in /usr/local/apache2/htdocs/myfile.php PHP Warning: Invalid argument supplied for foreach() in /usr/local/apache2/htdocs/myfile.php PHP Warning: Invalid argument supplied for foreach() in... (1 Reply)
Discussion started by: shantanuo
1 Replies

9. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

10. Shell Programming and Scripting

How to cut prefix from a string?

Hi folks, I have the following parameter setting: export ADMIN_HOST_NAME=http://hostname.com I want to define a new parameter,ADMIN_HOST_NAME_NEW,which based on $ADMIN_HOST_NAME but I need to remove the prefix "http://". The requested result for $ADMIN_HOST_NAME_NEW is hostname.com How... (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question