Fix timestamp with Sed or Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fix timestamp with Sed or Awk
# 1  
Old 05-16-2011
Fix timestamp with Sed or Awk

Hi
I am dealing with the following string:

Date: Thur, 13 March 2011 01:01:10 +0000

I asked for help in another topic that converted a similar string:

Date: Thur, 13 March 2011 9:50 AM

To a 24 hr standard. The problem is that it comes out as:

Date: Thur, 13 March 2011 9:50:00 +0000

I need to have it so that the first number in the time stamp ( in this case 9) to appear as 09:50:00.

Basicly if the first set of number in the time stamp is only 1 character long then append a 0 at the start.

This is the code used to convert the 12hr clock to 24hr:

Code:
awk '/PM/{split($6,_1,":");$6=_1[1]+12":"_1[2]}/AM|PM/{$6=$6":00";$7=$8;NF=7}1' infile

I thought I could fix it by replacing
Code:
{$6=$6

with
Code:
{$6=0$6

but no go as it appends a 0 at the start even when the time is PM

Thanks
# 2  
Old 05-16-2011
Try:
Code:
perl -pe 's/\b\d:\d\d:\d\d\b/0$&/g' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 05-16-2011
thank you!
I havent really touch much of perl, what is it that it is doing?
# 4  
Old 05-16-2011
It is matching any string in form of "*:**:**", where stars are digits, and it is putting 0 in front of it. \b (word border) at the beginning of the match makes sure that only single digit number in first position is matched.
# 5  
Old 05-16-2011
or you can try wıth sed Smilie

Code:
# cat file
Date: Thur, 13 March 2011 9:50:00 +0000

Code:
# sed 's/\(.*\)\(\b[0-9]\)\(:[^ ]*:[^ ]*.*\)/\10\2\3/' file
Date: Thur, 13 March 2011 09:50:00 +0000

it is find your part of string within 9:50:00 if it has single char in "9:.*:.*"
and append a zero to beginning its.

\(.*\) portion of full string so that `Date: Thur, 13 March `
\(\b[0-9]\) portion of full string so that `9`
\(:[^ ]*:[^ ]*.*\) portion of full string so that `50:00 +0000`

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed with Timestamp

Hi, I would like to replace a "." with timestamp. Constraint : I am making use of an existing shell script(called by a C program ) which reads the sed command from a properties file.I cant use/pass variables Line in C program - $(echo ${filename} | sed "$TRANSFORM" $TRANSFORM is populated... (4 Replies)
Discussion started by: mohanpadamata
4 Replies

2. Shell Programming and Scripting

sed finds nothing but it changes file's timestamp

I must go through some files to change a certain string within text files to another string. I use openSUSE and folders are mounted by cifs. Text to be replaced (only in .m extension) is U:\FOLDER and new string is N: That works fine with spaces in directory names etc., but this process ... (5 Replies)
Discussion started by: Pappa41
5 Replies

3. UNIX for Dummies Questions & Answers

Using SED to fix base64_decode attack/hack

last night our server was hit with an attack that infected every php file on the server and inserted the following code /*god_mode_on*/eval(base64_decode with a ton of other characters after. As it infected every php file i have been trying to clean it using a sed command to go through... (2 Replies)
Discussion started by: derrickyoung95
2 Replies

4. UNIX for Dummies Questions & Answers

Format/Fix Timestamp Data in a File.

Hello Experts, I have a timestamp(6) column in a .csv data file , format of the data is as below:- ETCT,P,Elec, Inc.,abc,11/5/2010 4:16:09.000000 PM,Y,Y,Y I want the timestamp column to be properly formatted like 11/05/2010 04:16:09.000000 PM Currently the "0" is missing with... (3 Replies)
Discussion started by: mtlrsk
3 Replies

5. Shell Programming and Scripting

How to fix this awk

I have a script which will mask the 9th and 15th column in a record starting with BPR. The record looks like below before my script BPR*C*160860.04*C*ACH*CTX*01*072000326*DA*1548843*3006968523**01*071000013*DA*5529085*100323*VEN The record will be masked after my script parses this... (19 Replies)
Discussion started by: Muthuraj K
19 Replies

6. Shell Programming and Scripting

Separate date timestamp use awk or sed command ?

Hi, I have logfile like this : Actually the format is date format : yyyymmddHHMMSS and i want the log become this format yyyy-mm-dd HH:MM:SS for example 2009-07-19 11:46:52 Can somebody help me ? Thanks in advance (3 Replies)
Discussion started by: justbow
3 Replies

7. Shell Programming and Scripting

how to fix this awk script?

i have a log file while looks like this ++ user_a blabla blabla nas_b blabla user_d this is a user_a junk line another junk line user_c nas_m blabla ++ basically most of the lines contain a "user" keywords, and the rest of the lines do not have "user" at all. So I have the... (17 Replies)
Discussion started by: fedora
17 Replies

8. Linux

sed to fix view names

I have a ddl file which have lots of view in it. I want to replace all the existing views with VW_< view name> . I am prefixing VW to existing view name . For example, In old file grep on view is like this CREATE VIEW OPSDM001.PROVIDER_MBR_PRI ( MBR_PRI_PROV_SYS_ID,... (6 Replies)
Discussion started by: capri_drm
6 Replies

9. Linux

SED/AWK Script to clear log file using timestamp?

I have a log file on our system which fills up with lines that have been timestamped, as follows.... 03/03/2008 10:56:06:815] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:06:820] (ERROR) balance: continuing session to genapp02 : 18500 03/03/2008 10:56:07:003]... (2 Replies)
Discussion started by: davesimm
2 Replies

10. UNIX for Advanced & Expert Users

Deleting timestamp using sed command

Hi, I'm trying to compare Actual.html with a baseline.html However, everytime it fails b'coz of the timestamp differences between the two. So, thought of stripping off the timestamp from both the *html files before comparing using below sed command over Solaris Unix platform:... (3 Replies)
Discussion started by: elearn.latha
3 Replies
Login or Register to Ask a Question