Sponsored Content
Full Discussion: Cut only time from string
Top Forums UNIX for Dummies Questions & Answers Cut only time from string Post 302955715 by RavinderSingh13 on Monday 21st of September 2015 04:35:14 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy