Sponsored Content
Top Forums Shell Programming and Scripting Sed/awk command to convert number occurances into date format and club a set of lines Post 302957570 by Chinmaya Kabi on Tuesday 13th of October 2015 03:19:24 AM
Old 10-13-2015
Sed/awk command to convert number occurances into date format and club a set of lines

Hi,

I have been stuck in this requirement where my file contains the below format.

Code:
20150812170500846959990854-25383-8.0.0
"ABC Report" hp96880
"4952"
20150812170501846959990854-25383-8.0.0 End of run
20150812060132846959990854-20495-8.0.0
"XYZ Report" vg76452
"1006962188"
20150812060141846959990854-20495-8.0.0
"ZZY Report" fu59172
20150812060147846959990854-20495-8.0.0 End of run

It follows the below pattern.
Line 1: Start Time
Line 2: Report Name and User
Line 3: Identifier
Line 4: End Time
In the following lines, the 2nd block is missing the End Time and the 3rd block is missing the Identifier.

The requirement is to
1. convert all lines starting with "20" into date format i.e. YYYY/MM/DD
2. Merge block from Start Time till End time separated by commas.
3. Ignore blocks that that don't have the end time.
4. Add a blank space in the block which doesn't contain identifier.
5. If possible, separate Report Name and User Name with comma.

The output should basically look like the below.
Code:
2015/08/12:17:05:00,"ABC Report",hp96880,"4952",2015/08/12:17:05:01
2015/08/12:06:01:41,"ZZY Report",fu59172,"",2015/08/12:06:01:47


I used the if loop for addressing the requirements but the script slows down when run for large files and hence I'm looking for a faster solution using sed or awk.
Can anyone please help me out here ?

Last edited by Scrutinizer; 10-13-2015 at 04:23 AM.. Reason: code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

2. Shell Programming and Scripting

Format of SED command to change a date

I have a website. I have a directory within it with over a hundred .html files. I need to change a date within every file. I don't have an easy way to find/replace. I need to change 10/31 to 11/30 on every single page at once. I tried the command below but it didn't work. Obviously I don't know... (3 Replies)
Discussion started by: ijustsawmars
3 Replies

3. Shell Programming and Scripting

sed remove multiple set of lines in one command

is there a way with sed to removed more than one set of lines in one line? so i mean sed ${firstElem},${lastIndex}d web.xml > web1.xml this will delete lines between ${firstElem},${lastIndex} i want in the same line to do somethinkg like this (doesn't work so far) sed... (3 Replies)
Discussion started by: Poki
3 Replies

4. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

5. Shell Programming and Scripting

awk or sed - Convert 2 lines to 1 line

Hi, Just trying to get to grips with sed and awk for some reporting for work and I need some assistance: I have a file that lists policy names on the first line and then on the second line whether the policy is active or not. Policy Name: Policy1 Active: yes Policy... (8 Replies)
Discussion started by: guinch
8 Replies

6. Shell Programming and Scripting

How to convert date format such as 7/18/2015 to the number of month

How to convert date format such as 7/18/2015 to the number of month from requesting date 'date' in sh scripting ? Let say I have output in my log.txt -> 7/18/2015. How I convert it to the full number of month starting from 'date' till 7/18/2015 in shell scripting ? Thanks in advance. (1 Reply)
Discussion started by: amerabest
1 Replies

7. Shell Programming and Scripting

Display date in mm/dd/yy format in sed command

Hi All, Following is my issue. $MAIL_DOC = test.txt test.txt contains the following text . This process was executed in the %INSTANCE% instance on %RUNDATE%. I am trying to execute the following script var=`echo $ORACLE_SID | tr ` NOW=$(date +"%D") sed -e... (3 Replies)
Discussion started by: megha2525
3 Replies

8. Shell Programming and Scripting

awk convert date format

Could you tell me how to convert the following dates? If I have m/d/yyyy, I want to have 0m/0d/yyyy. I want my dates to always be 8 digits. In other words, I want a 0 inserted whenever the month or day is a single digit. My issue is first I need to use FS="," to get field $4 for the... (7 Replies)
Discussion started by: wbrunc
7 Replies

9. Shell Programming and Scripting

sed command to replace slash in date format only

Hello experts. I haven't been able to find a solution for this using the sed command. I only want to replace the forward slash with string "FW_SLASH" only if there's a number right after the slash while preserving the original number. I have a file containing 2 entries: Original File:... (5 Replies)
Discussion started by: pchang
5 Replies

10. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies
Log::Report::Dispatcher::Try(3pm)			User Contributed Perl Documentation			 Log::Report::Dispatcher::Try(3pm)

NAME
Log::Report::Dispatcher::Try - capture all reports as exceptions INHERITANCE
Log::Report::Dispatcher::Try is a Log::Report::Dispatcher SYNOPSIS
try { ... }; # mind the ';' !! if($@) { # signals something went wrong if(try {...}) { # block ended normally my $x = try { read_temperature() }; my @x = try { read_lines_from_file() }; try { ... } # no comma!! mode => 'DEBUG', accept => 'ERROR-'; try sub { ... }, # with comma mode => 'DEBUG', accept => 'ALL'; try &myhandler, accept => 'ERROR-'; print ref $@; # Log::Report::Dispatcher::Try $@->reportFatal; # re-dispatch result of try block $@->reportAll; # ... also warnings etc if($@) {...} # if errors if($@->failed) { # same # } if($@->success) { # no errors # } try { # something causes an error report, which is caught report {to => 'stderr'}, FAILURE => 'no network'; }; $@->reportFatal(to => 'syslog'); # overrule destination print $@->exceptions; # no re-cast, just print DESCRIPTION
The Log::Report::try() catches errors in the block (CODE reference) which is just following the function name. All dispatchers are temporarily disabled by "try", and messages which are reported are collected within a temporary dispatcher named "try". When the CODE has run, that "try" dispatcher is returned in $@, and all original dispatchers reinstated. Then, after the "try" has finished, the routine which used the "try" should decide what to do with the collected reports. These reports are collected as Log::Report::Exception objects. They can be ignored, or thrown to a higher level try... causing an exit of the program if there is none. METHODS
Constructors $obj->close Only when initiated with a FILENAME, the file will be closed. In any other case, nothing will be done. Log::Report::Dispatcher::Try->new(TYPE, NAME, OPTIONS) -Option --Defined in --Default accept Log::Report::Dispatcher depend on mode charset Log::Report::Dispatcher <undef> died undef exceptions [] format_reason Log::Report::Dispatcher 'LOWERCASE' locale Log::Report::Dispatcher <system locale> mode Log::Report::Dispatcher 'NORMAL' accept => REASONS charset => CHARSET died => STRING The exit string ($@) of the eval'ed block. exceptions => ARRAY-of-EXCEPTIONS format_reason => 'UPPERCASE'|'LOWERCASE'|'UCFIRST'|'IGNORE'|CODE locale => LOCALE mode => 'NORMAL'|'VERBOSE'|'ASSERT'|'DEBUG'|0..3 Accessors $obj->died([STRING]) The message which was reported by "eval", which is used internally to catch problems in the try block. $obj->exceptions Returns all collected "Log::Report::Exceptions". The last of them may be a fatal one. The other are non-fatal. $obj->isDisabled See "Accessors" in Log::Report::Dispatcher $obj->mode See "Accessors" in Log::Report::Dispatcher $obj->name See "Accessors" in Log::Report::Dispatcher $obj->needs See "Accessors" in Log::Report::Dispatcher $obj->type See "Accessors" in Log::Report::Dispatcher Logging $obj->collectLocation Log::Report::Dispatcher::Try->collectLocation See "Logging" in Log::Report::Dispatcher $obj->collectStack([MAXDEPTH]) Log::Report::Dispatcher::Try->collectStack([MAXDEPTH]) See "Logging" in Log::Report::Dispatcher $obj->log(OPTS, REASON, MESSAGE) Other dispatchers translate the message here, and make it leave the program. However, messages in a "try" block are only captured in an intermediate layer: they may never be presented to an end-users. And for sure, we do not know the language yet. The MESSAGE is either a STRING or a Log::Report::Message. $obj->reportAll(OPTIONS) Re-cast the messages in all collect exceptions into the defined dispatchers, which were disabled during the try block. The OPTIONS will end-up as HASH-of-OPTIONS to Log::Report::report(); see Log::Report::Exception::throw() which does the job. $obj->reportFatal Re-cast only the fatal message to the defined dispatchers. If the block was left without problems, then nothing will be done. The OPTIONS will end-up as HASH-of-OPTIONS to Log::Report::report(); see Log::Report::Exception::throw() which does the job. $obj->stackTraceLine(OPTIONS) Log::Report::Dispatcher::Try->stackTraceLine(OPTIONS) See "Logging" in Log::Report::Dispatcher $obj->translate(HASH-of-OPTIONS, REASON, MESSAGE) See "Logging" in Log::Report::Dispatcher Status $obj->failed Returns true if the block was left with an fatal message. $obj->showStatus If this object is kept in $@, and someone uses this as string, we want to show the fatal error message. The message is not very informative for the good cause: we do not want people to simply print the $@, but wish for a re-cast of the message using reportAll() or reportFatal(). $obj->success Returns true if the block exited normally. $obj->wasFatal(OPTIONS) Returns the Log::Report::Exception which caused the "try" block to die, otherwise an empty LIST (undef). -Option--Default class undef class => CLASS|REGEX Only return the exception if it was fatal, and in the same time in the specified CLASS (as string) or matches the REGEX. See Log::Report::Message::inClass() OVERLOADING
overload: boolean Returns true if the previous try block did produce a terminal error. This "try" object is assigned to $@, and the usual perl syntax is "if($@) {...error-handler...}". overload: stringify When $@ is used the traditional way, it is checked to have a string content. In this case, stringify into the fatal error or nothing. SEE ALSO
This module is part of Log-Report distribution version 0.94, built on August 23, 2011. Website: http://perl.overmeer.net/log-report/ LICENSE
Copyrights 2007-2011 by Mark Overmeer. For other contributors see ChangeLog. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html perl v5.14.2 2011-08-23 Log::Report::Dispatcher::Try(3pm)
All times are GMT -4. The time now is 04:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy