Cut only time from string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cut only time from string
# 1  
Old 09-03-2015
Cut only time from string

Hi Guys,

I have following different lines separated by multiple spaces. And i need to cut only the time part (16:53) from the string. How can we achieve it?

Code:
remedy   29210     1  1 07:55 ?        00:06:20 /opt/bmc/java/jre1.6.0_17/bin/java -Xms1024m -Xmx2048m -XX:MaxPermSize=1024m -Djava.awt.headless=true

Code:
remedy   25883     1  2 07:53 ?        00:10:46 /opt/bmc/java/jre1.6.0_17/bin/java -Xms1024m -Xmx2048m -XX:MaxPermSize=1024m -Djava.awt.headless=true

There are 3 spaces between 'remedy' and '29210' aqnd likewise goes on.

Please let me know how can we cut only 07:55 and 07:53 from these lines.

Thanks,
Khushbu.
# 2  
Old 09-03-2015
Try:
Code:
awk '{print $5}' file

if it is a single line string, (in bash/ksh93/zsh) you could also use:
Code:
read x x x x time x <<< "$var"

or in general you could use:
Code:
read x x x x time x << EOF
$var
EOF

Which would put the time in the variable time
# 3  
Old 09-04-2015
Try extending Scrutinizer's awk proposal:
Code:
awk '$5 ~ /07:53|07:55/{print $5}' file

# 4  
Old 09-04-2015
Hi RudiC,

The time will be runtime value. I cannot pass this static value through this as illustrated in your example.
I need the 5th field (07:55) to be cut which is separated by multiple spaces.
Please help.
# 5  
Old 09-04-2015
Hello Khushbu,

If time comes always in 5th field then you can try following.
Code:
awk '{print $5}' Input_file

If time's field is not set then you could try following.
Code:
awk --re-interval '{match($0,/[0-9]{2}:[0-9]{2}/);print substr($0,RSTART,RLENGTH)}' Input_file

Hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 09-04-2015
@Khushbu. Have you tried any of the suggestions? What is the result? Please provide some feedback.
# 7  
Old 09-21-2015
Hi Ravinder,

Thanks a ton for your previous answer to my post regarding grep time form string. Its working amazingly!

Now, i have stored this time in lastrestart.txt file as follows:

Code:
07:25


Also, i have stored current system time in now.txt file as follows:


Code:
09:38

Now, i want to calculate the difference between these two times in hours only, in a variable. We follow 24-hr format time and hence would need to consider that format too.

Can you please help me in this?

Awaiting your response Smilie
 
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