To extract date and time separately from the file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To extract date and time separately from the file name
# 1  
Old 06-04-2010
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.


Code:
 
#!/bin/bash
echo "Enter the file name to extract the timestamp"
read fname
IFILE=$fname
F=$IFILE
IFS="_."  
f=($F)    
echo "Date ${f[2]} Time ${f[3]}"

How to generalize the script to extract the fields regardless of its position.

Currently for my i/p file of format: ot_abc_20100406_041025.txt, it is working properly. And also my timestamp will be last 2 fields before the file extension starts.

So regardless of i/p file I want the script to extract the last to fields. Request you to suggest the solution.

Thank you.
# 2  
Old 06-04-2010
You can do something like:
Code:
#!/bin/bash

echo "Enter the file name to extract the timestamp"
read fname

set `echo "$fname"| sed 's/.*_\(.*\)_\(.*\)\..*/\1 \2/'`
echo $1
echo $2

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 06-04-2010
Hi.,

Thanks for your reply. The script is working properly.
Here can you tell me what set command is doing, as I am new to Shell scripting. And also how you are extracting last 2 fields using stream editor cmd?

Thank you.
# 4  
Old 06-04-2010
Quote:
Originally Posted by IND123
Hi.,

Thanks for your reply. The script is working properly.
Here can you tell me what set command is doing, as I am new to Shell scripting. And also how you are extracting last 2 fields using stream editor cmd?

Thank you.
From an excerption of man set:

Quote:
Unless -A is specified, the remaining arguments are
positional parameters and are assigned, in order, to $1 $2 .... If no
arguments are specified then the names and values of all variables are
printed on the standard output.
For the sed command you can have a read of this:

Sed - An Introduction and Tutorial
This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract date and time part from filename

Hi, I am facing one scenario in which I need to extract exact position of date and time from the name of the files. For example, Below is the record in which I need to extract position of YYYYMMDD,HHMISS and YYMMDD. Date and time variables can come more than once. I need to use these position... (13 Replies)
Discussion started by: Prathmesh
13 Replies

2. UNIX for Beginners Questions & Answers

How to extract date and time from filename?

Hi, I'm totally new in sell script and working with a shell code. I want to extract the date and time from the filenames. The filenames are different but all of them begins with WI_ SCOPE_: WI_SCOPE_DATA_CHANGE_2017-09-12_15-30-40.txt WI_SCOPE_BACK_COMPLETE_QUEUE_2017-09-12_15-31-40.txt... (5 Replies)
Discussion started by: Home
5 Replies

3. Shell Programming and Scripting

Extract date / time

How do i display in the below format without the brackets using shell script. Tue Oct 1 13:32:40 2013 Please use CODE tags not only for all code segments, input samples, and output samples. (7 Replies)
Discussion started by: ghosh_tanmoy
7 Replies

4. Shell Programming and Scripting

How to extract latest file by looking at date time stamp from a directory?

hi, i have a Archive directory in which files are archived or stored with date and time stamp to prevent over writing. example: there are 5 files s1.txt s2.txt s3.txt s4.txt s5.txt while moving these files to archive directory, date and time stamp is added. of format `date... (9 Replies)
Discussion started by: Little
9 Replies

5. UNIX for Dummies Questions & Answers

Breaking a fasta formatted file into multiple files containing each gene separately

Hey, I've been trying to break a massive fasta formatted file into files containing each gene separately. Could anyone help me? I've tried to use the following code but i've recieved errors every time: for i in *.rtf.out do awk '/^>/{f=++d".fasta"} {print > $i.out}' $i done (1 Reply)
Discussion started by: Ann Mc Cartney
1 Replies

6. Shell Programming and Scripting

Extract info from log file and compute using time date stamp

Looking for a shell script or a simple perl script . I am new to scripting and not very good at it . I have 2 directories . One of them holds a text file with list of files in it and the second one is a daily log which shows the file completion time. I need to co-relate both and make a report. ... (0 Replies)
Discussion started by: breez_drew
0 Replies

7. Shell Programming and Scripting

To extract data of a perticular interval (date-time wise)

I want a shell script which extract data from a log file which contains date and time-wise data and i need the data for a perticular interval of time...what can i do??? (3 Replies)
Discussion started by: abhishek27
3 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

Processing a log file based on date/time input and the date/time on the log file

Hi, I'm trying to accomplish the following and would like some suggestions or possible bash script examples that may work I have a directory that has a list of log files that's periodically dumped from a script that is crontab that are rotated 4 generations. There will be a time stamp that is... (4 Replies)
Discussion started by: primp
4 Replies

10. Shell Programming and Scripting

how to print the date and time separately???

HI, I have a script where the date and time has to e printed separately as below date = "Wed Jan 16 2008" time = "14:17:57 IST" using date command gives me tho out put Wed Jan 16 14:17:57 IST 2008 i need only Wed Jan 16 2008 and 14:17:57 IST both stored in two different variables.... (8 Replies)
Discussion started by: jisha
8 Replies
Login or Register to Ask a Question