Change the seconds value in date column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change the seconds value in date column
# 1  
Old 06-28-2014
Change the seconds value in date column

Hello,

I have a file with below contents and need to set seconds value to 00 (as you can see few time stamps are with 01 seconds)
Code:
18:16:00        8192    7301    89      11
18:21:00        8192    7305    89      11
18:26:00        8192    7306    89      11
18:31:00        8192    7306    89      11
18:36:00        8192    7306    89      11
18:41:01        8192    7306    89      11
18:46:01        8192    7306    89      11
18:51:00        8192    7306    89      11
18:56:01        8192    7306    89      11
19:01:00        8192    7301    89      11
19:06:01        8192    7306    89      11
19:11:00        8192    7306    89      11
19:16:00        8192    7306    89      11
19:21:01        8192    7308    89      11
19:26:00        8192    7306    89      11
19:31:00        8192    7306    89      11
19:36:00        8192    7306    89      11
19:41:00        8192    7306    89      11

I tried sed 's/:01$/:00/g' but it works only if I have one column with time stamps.

Please advise
Thank you,
# 2  
Old 06-28-2014
Drop the EOL indicator, and don't use the g option:
Code:
sed 's/:01 /:00 /' file

# 3  
Old 06-28-2014
You said it only works with one column with timestamps, but as RudiC said, your code only works if there is only one column with timestamps and that timestamp is at the end of the line. RudiC's code works for one timestamp anywhere on a line except at the end of a line (and won't work if you have tabs as field separators instead of spaces).

As long as you have a version of sed that conforms to the standards, the following should work no matter how many timestamps you have on a line, no matter where they are placed on a line, and no matter what field separators you use:
Code:
sed 's/\([0-2][0-9]:[0-5][0-9]:\)[0-9][0-9]/\100/g' file

If file contains:
Code:
18:16:00        8192    7301    89      11
18:21:00        8192    7305    89      11
18:26:00        8192    7306    89      11
18:31:00        8192    7306    89      11
18:36:00        8192    7306    89      11
18:41:01        8192    7306    89      11
18:46:01        8192    7306    89      11
18:51:00        8192    7306    89      11
18:56:01        8192    7306    89      11
19:01:00        8192    7301    89      11
19:06:01        8192    7306    89      11
19:11:00        8192    7306    89      11
19:16:00        8192    7306    89      11
19:21:01        8192    7308    89      11
19:26:00        8192    7306    89      11
19:31:00        8192    7306    89      11
19:36:00        8192    7306    89      11
19:41:00        8192    7306    89      11
01:02:03	04:05:06	07:08:09	10:11:12	13:14:15

the output produced is:
Code:
18:16:00        8192    7301    89      11
18:21:00        8192    7305    89      11
18:26:00        8192    7306    89      11
18:31:00        8192    7306    89      11
18:36:00        8192    7306    89      11
18:41:00        8192    7306    89      11
18:46:00        8192    7306    89      11
18:51:00        8192    7306    89      11
18:56:00        8192    7306    89      11
19:01:00        8192    7301    89      11
19:06:00        8192    7306    89      11
19:11:00        8192    7306    89      11
19:16:00        8192    7306    89      11
19:21:00        8192    7308    89      11
19:26:00        8192    7306    89      11
19:31:00        8192    7306    89      11
19:36:00        8192    7306    89      11
19:41:00        8192    7306    89      11
01:02:00	04:05:00	07:08:00	10:11:00	13:14:00

# 4  
Old 06-28-2014
Thanks RudiC and Don Cragun.

@Don Cragun - You are right. RudiC's solution did not work. I'm running on AIX 6 ksh.

Your solution worked great. In your solution, the one within () would remain be unchanged by sed? instead only the latter part replaced? also what does \1 (before 00) do?

Pl explain, thank you again!
# 5  
Old 06-28-2014
Do you have TABs as field separators?
# 6  
Old 06-28-2014
The \1 prints what matched within the first ( ).
# 7  
Old 06-28-2014
Quote:
Originally Posted by RudiC
Do you have TABs as field separators?
Given the spacing between fields in the sample input, I was guessing that tabs could have been converted to spaces in the copy and paste process. redder will have to let us know whether the real data has spaces, tabs, or a mix.

I chose a BRE that would work either way.

To expand on what MadeInGermany said: \digit expands to the string that was matched by the BRE between the \( and the matching \) where the digit (1 through 9) refers to the 1st through the 9th \( to indicate which matched expression is to be included in the replacement text. In the expression I gave, \1 expands to HH:MM: from the matched timestamp. So \100expands to the matched hours and minutes with the seconds replaced by 00.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subtract Seconds from Date Command

Hi, Need to subtract 5 seconds after syncing my Linux server from NTP like; #ntpdate time.myorg.int. This script will only run once in each morning at 9 AM. Please help me. (4 Replies)
Discussion started by: refra
4 Replies

2. Shell Programming and Scripting

Regex match date and seconds format

Hi $ awk '{print $1," ",$4}' access.log | sort | uniq -c| sort -nr | head -n20 62 192.168.10.6 How can get the result like 62, 192.168.10.6, 14:40 62, 192.168.10.32, 47:57 I tried modifying - $ awk '{print $1," ",$4}' access.log | sort | uniq -c| sort -nr | head -n20 | awk... (3 Replies)
Discussion started by: ashokvpp
3 Replies

3. Shell Programming and Scripting

How to change the format of the date column in a flat file?

Hi, i have a flat file namely temp.txt with this data below ID|name|contact_date 101|Kay|2013-12-26 102|let|2013-12-26 I need to modify the date data in the flat file into MM/DD/YYYY HH24:MI:SS format let me know the code for this. Thank you! (5 Replies)
Discussion started by: srikanth_sagi
5 Replies

4. Shell Programming and Scripting

Getting date in seconds with decimals

I am trying to get date to display decimal Desired output 1350386096256.12 I know this can be done with printf, but are not able to make it work. I have tested this and many otherprintf "%.2f" $(($(date +%s%N)/1000000)) (8 Replies)
Discussion started by: Jotne
8 Replies

5. Shell Programming and Scripting

Date format in micro seconds

Can i get date format in micro seconds in unix example 2012-01-27- 12.22.04.568722 Any help is appreciable (2 Replies)
Discussion started by: srichunduru
2 Replies

6. UNIX for Dummies Questions & Answers

Current system date in terms of seconds

Hello Friends, I've been struggling with extreme nagios passive service checks. In order to trigger a nagios passive service im going to write an easy shell script like below and will run it in crontab. As im working on Solaris 10 servers i used "S" instead of lowercase "s" below #!/bin/sh... (2 Replies)
Discussion started by: EAGL€
2 Replies

7. Shell Programming and Scripting

Converting past date to seconds

Need a little help developing a ksh script. Have read through Perderabo's datecalc routine and it does not seem to fit the function I am looking for. Basically what I am trying to do is find any file (in a specific directory) that was created within the last five minutes. I am not a programming... (3 Replies)
Discussion started by: synergy_texas
3 Replies

8. HP-UX

a simple way of converting a date in seconds to normal date

Hi all! I'm working on a HPUX system, and I was wondering if there is a simple way to convert a date from seconds (since 1970) to a normal date. Thanks (2 Replies)
Discussion started by: travian
2 Replies

9. Solaris

how to get the milli seconds from date in sun unix

hi all how do we get the milli seconds in sun unix? i am using date +%Y%m%d%H%M%S to get the unique id and create the file based on this. but the problem is that if process to load the table takes only less than 1 sec i am getting errror on my table which have the primary key. how... (2 Replies)
Discussion started by: r2b
2 Replies

10. UNIX for Advanced & Expert Users

file creation date including seconds

Hi, Is anybody can help me to get the file creation date with seconds? -rw-r--r-- 1 opsc system 422550845 Aug 22 15:41 StatData.20020821 Thanks in advance Krishna (7 Replies)
Discussion started by: krishna
7 Replies
Login or Register to Ask a Question