Sponsored Content
Top Forums Shell Programming and Scripting Check if a date field has date or timestamp or date&timestamp Post 302602073 by durden_tyler on Saturday 25th of February 2012 10:29:27 PM
Old 02-25-2012
Quote:
Originally Posted by machomaddy
...
If it is date then append "<space>00:00:00" to it.
If it is a time stamp, store the data in a different file(invalid_dttmstmp.txt) and remove from this file.
If it is a date timestamp then do nothing.
Input file:
Code:
File1
====
Field 1
=====
00:00:00
<space>01:01:01
2012-02-25 00:30:14
2012-02-25

Expected output
Code:
File1
====
Field 1
=====
2012-02-25 00:30:14
2012-02-25 00:00:00

invalid_dttmstmp.txt
===============
Field 1
======
00:00:00
<space>01:01:01

...
Here's a Perl program that does all that.

Code:

$
$ cat -n file1
     1  Field 1
     2  =====
     3  00:00:00
     4   01:01:01
     5  2012-02-25 00:30:14
     6  2012-02-25
$
$ cat -n process_dates.pl
     1  #!perl -w
     2  use File::Copy;
     3  my $orig = "file1";                # the original file
     4  my $bkup = "file1.bak";            # the backup, just in case
     5  my $temp = "file1.tmp";            # valid records go here
     6  my $invl = "invalid_dttmstmp.txt"; # and invalid go here
     7  copy ($orig, $bkup)  or die "Can't back up original file: $!";
     8  open (V, ">", $temp) or die "Can't open $temp for writing: $!";
     9  open (I, ">", $invl) or die "Can't open $invl for writing: $!";
    10  open (F, "<", $orig) or die "Can't read original file $orig: $!";
    11  while (<F>) {
    12    if (/^(Field|=+)/) {print I $_; print V $_}
    13    elsif (/^\s*\d+:/) {print I $_}
    14    elsif (/^\d{4}-\d\d-\d\d$/) {chomp; print V "$_ 00:00:00\n"}
    15    elsif (/^\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d$/) {print V $_}
    16  }
    17  close (V)    or die "Can't close $temp: $!";
    18  close (I)    or die "Can't close $invl: $!";
    19  rename ($temp, $orig);
$
$ perl process_dates.pl
$
$ # check the backup file
$ cat -n file1.bak
     1  Field 1
     2  =====
     3  00:00:00
     4   01:01:01
     5  2012-02-25 00:30:14
     6  2012-02-25
$
$ # check the original file
$ cat -n file1
     1  Field 1
     2  =====
     3  2012-02-25 00:30:14
     4  2012-02-25 00:00:00
$
$ # check the invalid data file
$ cat -n invalid_dttmstmp.txt
     1  Field 1
     2  =====
     3  00:00:00
     4   01:01:01
$
$

tyler_durden
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

get a file date/timestamp

Could someone tell me how to get the date/time (to the second) a file was last modified? I need to know if a file was modified in the last 30 seconds from the system date. I'm on AIX/unix 4.3 (3 Replies)
Discussion started by: alex31
3 Replies

2. UNIX for Dummies Questions & Answers

getting date from timestamp

Hi All, i have a time stamp. from that i am trying to awk to get the year, month and date. TIME=20060614092446 DESIRED OUTPUT: 20060614 i am doing the following; TIME=20060614092446 $ TimeStarted=`expr match '$TIME' '.*\(......\)'` echo $TimeStarted i am not getting... (2 Replies)
Discussion started by: pavan_test
2 Replies

3. UNIX for Dummies Questions & Answers

vmstat output with date & timestamp

Hello all This is a sample vmstat output ... $ vmstat 2 2 kthr memory page disk faults cpu r b w swap free re mf pi po fr de sr hx hx hx hx in sy cs us sy id 1 0 0 23105784 7810488 323 767 1742 5 5 0 0 0 0 0 0 683 780 457 43 ... (9 Replies)
Discussion started by: luft
9 Replies

4. Shell Programming and Scripting

File renaming with date timestamp

Hi, This is my script: #! /usr/bin/ksh cd /app/chdata/workflow/suppl/esoutput/spd/testing for file in /app/chdata/workflow/suppl/esoutput/spd/testing do sort *.txt | awk '{ file=substr($0,1,2)".txt"; print >> file }' ... (3 Replies)
Discussion started by: Sunitha_edi82
3 Replies

5. Shell Programming and Scripting

Timestamp & date

Hi, I have list of files as below, with prefix named as date & time. Anyone how to transform each file as below to yyyy-mm-dd hh:mm:ss Regards, (8 Replies)
Discussion started by: rauphelhunter
8 Replies

6. Shell Programming and Scripting

Timestamp to date conversion in ksh

Hi, I have a file containing timestamp( Example given below). How can i get date(mmd-dd-yyyy) from it? ($> cat file1.txt 2008-11-24 05:17:00.7043) Thanks, Sri (2 Replies)
Discussion started by: srilaxmi
2 Replies

7. UNIX for Dummies Questions & Answers

Unix timestamp to readable date

How would I convert a unix timestamp such as "1232144092" to a readable date such as "1/16/2009 10:14:28 PM" ? I thought I could use date, but I don't think so now.. Any help would be great!! (4 Replies)
Discussion started by: Rhije
4 Replies

8. Shell Programming and Scripting

How to convert date and timestamp?

Hi, I have a file file1 having data as below 20110501,070742, ,012345678909,09999999999,68.5, 20110501,070236, ,089375855455,09376383333,374.3, 20110501,070525, ,090345895555,08444233444,206.2, 20110501,230051, ,000934744433,07624262223,480.1, First field is date(YYYYMMDD) and second... (5 Replies)
Discussion started by: vsachan
5 Replies

9. Shell Programming and Scripting

File Timestamp and date comparsion

Hi, I have many files in the source directory but I need to process with the latest timestamp file. I am using linux operating system. i want extract the file created timestamp( Ext_File_create_date=) With this format YYYYMMDD- i have searched the relevent command in the unix forms but did... (5 Replies)
Discussion started by: onesuri
5 Replies

10. Shell Programming and Scripting

Replacing Date in the file with Create date and timestamp

Hello, I have files that with a naming convention as shown below. Some of the files have dates in the file name and some of them don't have dates in the file name. imap-hp-import-20150917.txt imap-dell-gec-import-20150901.txt imap-cvs-import-20150915.txt imap-gec-import.txt... (8 Replies)
Discussion started by: Saanvi1
8 Replies
JOIN(1) 						      General Commands Manual							   JOIN(1)

NAME
join - relational database operator SYNOPSIS
join [ options ] file1 file2 DESCRIPTION
Join forms, on the standard output, a join of the two relations specified by the lines of file1 and file2. If one of the file names is the standard input is used. File1 and file2 must be sorted in increasing ASCII collating sequence on the fields on which they are to be joined, normally the first in each line. There is one line in the output for each pair of lines in file1 and file2 that have identical join fields. The output line normally con- sists of the common field, then the rest of the line from file1, then the rest of the line from file2. Input fields are normally separated spaces or tabs; output fields by space. In this case, multiple separators count as one, and leading separators are discarded. The following options are recognized, with POSIX syntax. -a n In addition to the normal output, produce a line for each unpairable line in file n, where n is 1 or 2. -v n Like -a, omitting output for paired lines. -e s Replace empty output fields by string s. -1 m -2 m Join on the mth field of file1 or file2. -jn m Archaic equivalent for -n m. -ofields Each output line comprises the designated fields. The comma-separated field designators are either 0, meaning the join field, or have the form n.m, where n is a file number and m is a field number. Archaic usage allows separate arguments for field designators. -tc Use character c as the only separator (tab character) on input and output. Every appearance of c in a line is significant. EXAMPLES
sort /adm/users | join -t: -a 1 -e "" - bdays Add birthdays to password information, leaving unknown birthdays empty. The layout of is given in users(6); bdays contains sorted lines like tr : ' ' </adm/users | sort -k 3 3 >temp join -1 3 -2 3 -o 1.1,2.1 temp temp | awk '$1 < $2' Print all pairs of users with identical userids. SOURCE
/sys/src/cmd/join.c SEE ALSO
sort(1), comm(1), awk(1) BUGS
With default field separation, the collating sequence is that of sort -b -ky,y; with -t, the sequence is that of sort -tx -ky,y. One of the files must be randomly accessible. JOIN(1)
All times are GMT -4. The time now is 04:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy