How to extract date from a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to extract date from a file?
# 1  
Old 07-04-2014
How to extract date from a file?

Hello Gurus,
I have one file from which I want to extract only dates.
The below is the file format :
Code:
=========================
Process ID           16842770
Log Read Checkpoint  Oracle Integrated Redo Logs
2014-07-04 17:06:11  <===
SCN 1779.852353022 (7641599172606)

Process ID           31195320
Log Read Checkpoint  Oracle Integrated Redo Logs
2014-07-04 17:06:15 <===
SCN 1779.852354638 (7641599174222)

Checkpoint Lag       unknown (updated 00:00:04 ago)
Process ID           15794402
Log Read Checkpoint  Oracle Integrated Redo Logs
2014-07-04 17:06:16 <===
SCN 1779.852354836 (7641599174420)
=================================

Please advice

Thanks-
pokhraj das

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 07-04-2014 at 10:29 AM..
# 2  
Old 07-04-2014
Have you tried something? What you can think of for the approach?
# 3  
Old 07-04-2014
I have not tried anything..

Not sure...

Thanks-
pokhraj das
# 4  
Old 07-04-2014
Please wrap your input in [CODE] & [/CODE] tags. It makes it far easier to read and if the input might have multiple spaces, normal text will amalgamate those to a single, so we might give you wrong input.

A few questions though:-
  • What have you tried so far?
  • What error/output do you get?
  • What OS and version are you running?
  • What are your preferred tools (e.g. ksh, awk, etc.)
  • What logic would you use for a manual process?
  • Assuming that your dates are the lines with <=== in them, will your output actually contain these?
Most importantly, What have you tried so far?



Robin

---------- Post updated at 02:29 PM ---------- Previous update was at 02:26 PM ----------

Okay, so:-
Quote:
I have not tried anything..
What is your thinking at least? This isn't just a free service centre where you can get anything you want. We're all here to learn and part of that is that we offer help which gives us more to think about and so we learn more.

If you put in some effort, even if not coding, then you will get a better quality response. We can give you many ways to achieve what you state, but they may not do what you actually want and then you will also not learn how to do it yourself.



Robin
# 5  
Old 07-04-2014
I have tried the below method:
============
Code:
for i in `cat /home/oracle/extract_info_all|head -16|tail -1|awk '{print $1}'`
do
echo ${i}
done

========
but it is coming only one date which is at 16th line.
No other dates are coming so far.

I am using ksh



Thanks-
Pokhraj Das


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks
# 6  
Old 07-04-2014
... because you cut out line 16, while in reality you want to search for the date lines.
The task can be done with an RE (=regular expression):
[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]
matches the date format in your file.
Where [0-9] means: one character in the range 0..9 i.e. a digit.
grep "RE" returns the lines that match the RE.
The RE should be in quotes, so the shell does not try substitutions on it before passing it to grep.
# 7  
Old 07-10-2014
You can try out the below code:
Code:
a=$1
terminal=`tty`
exec < $a

while read line
do
        if [ -z "$line" ]
        then
                continue
        else
        set $line
        fi
        if [[ "$1" == *"-"*"-"* ]]
        then
                echo $1
        else
                continue
        fi
done

exec < $terminal

Provide the file name as part of the parameter. I guess that would solve your problem.

Moderator's Comments:
Mod Comment Please use code tags

Last edited by jim mcnamara; 07-10-2014 at 07:37 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract date from files based on file pattern

I want to extract dates from the files and i have different types of files with pattern. I have list file with the patterns and want to date extract based on it in a sh script Files in the directory : file1_20160101.txt file2_20160101_abc.txt filexyz20160101.txt list file with... (2 Replies)
Discussion started by: lijjumathew
2 Replies

2. UNIX for Dummies Questions & Answers

Extract date ranged data from log file

Hi, I am trying to extract lines of data within a log file on a Redhat 5 Linux system. eg I need all the lines with a particular username over the last 3 minutes. the log file may read like this, and I want a way to search all the lines extracting all the relevant lines over the last 3... (2 Replies)
Discussion started by: mantis100
2 Replies

3. Shell Programming and Scripting

Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server 20102009 is the date of the file ddmmaaaa format the complete route is /dprod/informatica/Fuentes/CRED20102009.csv i want to extract the date to create a new file named Parameters.txt I need to create Parameters.txt with this... (6 Replies)
Discussion started by: angel1001
6 Replies

4. Shell Programming and Scripting

To extract date and time separately from the file name

Hi., My current script extracts only if the date and time are in 3rd and 4th pos. #!/bin/bash echo "Enter the file name to extract the timestamp" read fname IFILE=$fname F=$IFILE IFS="_." f=($F) echo "Date ${f} Time ${f}" How to generalize the script to extract the... (3 Replies)
Discussion started by: IND123
3 Replies

5. Shell Programming and Scripting

Extract data from log file from or after the specific date

Hi , I am having a script which will start a process and appends the process related logs to a log file. The log file writes logs with every line starting with date in the format of: date +"%Y %b %d %H:%M:%S". So, in the script, before I start the process, I am storing the date as DATE=`date +"%Y... (5 Replies)
Discussion started by: chiru_h
5 Replies

6. Shell Programming and Scripting

Extract date from log file

Hello All, I just need to extract the date portion from a apache log file I am able to do it using the chain of command - Logfile contents - First record - ========================== 197.130.211.240 - - "GET /jp/index.shtml HTTP/1.1" 200 24255... (4 Replies)
Discussion started by: jambesh
4 Replies

7. Shell Programming and Scripting

Extract Expiry date and server_name from the following file

Hi Guys, I am trying to write one ksh script where I have to go to lots of unix boxes and read one text file which will have data like this though this is not complete data. And I just have to extract expire date ( which is 2nd date value 08/31/2009 in the first one ) and the server name ( astro... (0 Replies)
Discussion started by: pareshan
0 Replies

8. Shell Programming and Scripting

How to extract date with time from file

Hi, I have a file where there is a date and time field, the format for it is yyyy-mm-dd hours:mins:sec the position of date field may vary anywhere in the line and it might be different and it is specified along with the variable AppTimeStamp how do i extract date and time both from the... (5 Replies)
Discussion started by: prash_b
5 Replies

9. Shell Programming and Scripting

Extract Date from file

I am new to unix scripting. we are using bash. My task is I have dir which contains 30 files. The first column in each file is Date field. For all the files I need to extract the date part Ex(2007-09-05) from the file and add that at the end of that file. for example: The file names are... (2 Replies)
Discussion started by: magi
2 Replies

10. Shell Programming and Scripting

extract date portion from file

Hi, I have a file where there is a date field (single line variable length file) how to extract just the date portion from it the position of date field may vary anywhere in the line but will always have the format mm-dd-yyyy for eg . xxxxxxxxxxxxxxx09-10-2006xxxxxxxxxxxxxxxxxxxx (5 Replies)
Discussion started by: misenkiser
5 Replies
Login or Register to Ask a Question