How to extract timestamp from the filename?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract timestamp from the filename?
# 1  
Old 05-31-2010
How to extract timestamp from the filename?

Hi.,

My file name is of the format:

name_abc_20100531_142528.txt
where.,

my timestamp is of the format:
yyyymmdd_hhmmss

How to extract the date strring and time string into seperate variables in the shell script, after reading the file as the input?
I want to get the variables as in:

dateStr = 20100531
timeStr=142528

for furter processing for validation.


Thanks.,




---------- Post updated at 02:19 AM ---------- Previous update was at 02:04 AM ----------

Hi.,

I tried with :

Code:
 echo name_abc_20100531_142528.txt  | awk -F "_" '{print $3}' | read av ;echo $av

in shell itself, but failing to achieve the result, please help me out.


Thanks.,
# 2  
Old 05-31-2010
cut can do it too
Code:
 echo name_abc_20100531_142528.txt | cut -d_ -f3 | read av ;echo $av

This User Gave Thanks to Leion For This Post:
# 3  
Old 05-31-2010
Try:
Code:
echo name_abc_20100531_142528.txt |awk -F"[_.]" '{print $3, $4}'| read d t ;echo $d;echo $t

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 05-31-2010
I tried with the cut option, with which I am able to extract first date field, but it is not extracting time string as in :


Code:
dateStr=$(echo name_abc_20100531_142528.txt | cut -d_ -f3)

timeStr=$(echo name_abc_20100531_142528.txt | cut -d_ -f4)

echo $dateStr "   " $timeStr

Its printing:

20100531 142528.txt


But my desired o/p should be:

20100531 142528

Help me out to extract the field without the file extension.


Thanks.,
# 5  
Old 05-31-2010
Quote:
Originally Posted by av_vinay
I tried with the cut option, with which I am able to extract first date field, but it is not extracting time string as in :


Code:
dateStr=$(echo name_abc_20100531_142528.txt | cut -d_ -f3)

timeStr=$(echo name_abc_20100531_142528.txt | cut -d_ -f4)

echo $dateStr "   " $timeStr

Its printing:

20100531 142528.txt


But my desired o/p should be:

20100531 142528

Help me out to extract the field without the file extension.


Thanks.,

Franklin52's solution works better.
but i need to use nawk in solaris to get it to work.
Code:
echo name_abc_20100531_142528.txt |nawk -F"[_.]" '{print $3, $4}'| read d t ;echo $d;echo $t

On the cut, you can use
Code:
echo name_abc_20100531_142528.txt | cut -d_ -f4 | cut -d. -f1

which is less efficient..
This User Gave Thanks to Leion For This Post:
# 6  
Old 05-31-2010
Thanks for your help.
nawk version of the script worked for me.

Thanks once again.Smilie Smilie

---------- Post updated at 03:31 AM ---------- Previous update was at 02:43 AM ----------

Hi.,

I am with other concern about the same question which I initially posted.
Actually I tested the above things by hard coding name of the file in the script. But I want it to be dynamically placed into the script.

Actually after reading file name from the shell, the i/p var acts as the file as against to string. So how to treat the file _ name as the string?

Like in:

Code:
 
read fname
IFILE=$fname
 

dateStr=$(echo $IFILE | awk -F"[_]" '{print $3}')
timeStr=$(echo $IFILE | awk -F"[_.]" '{print $4}')
echo $dateStr "   " $timeStr

Kindly suggest the solution for the same.


Thanks.,
# 7  
Old 05-31-2010
With variable expansion
Code:
F="name_abc_20100531_142528.txt"
T=${F%.*}
D=${T#*_*_}
T=${T##*_}
D=${D%_*}
echo "TIME: $T - DATE: $D"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sort the timestamp in the filename in shell script?

originally the shellscript #ln_file_name=`echo $ld_interface_date"_"${8}".csv"` #ln_file_name=`echo 201202011527_HL_HLTM1_B04A.csv` ln_file_name="*"`echo ${7}".csv"` get_file_list_1=$log_path"tm1_file_list.gfl1" cd ${source_path} echo "Try to find any file exist in the... (10 Replies)
Discussion started by: feilhk
10 Replies

2. Shell Programming and Scripting

Filename timestamp

Hi Gurus, I have different files with different timestamp and different base file name, I have to group those files based on basename and provide a unique file name for similar file names. My Directory has following files. abc_filename_20130623:00:09:00.txt... (1 Reply)
Discussion started by: user_linux
1 Replies

3. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

5. Shell Programming and Scripting

Get filename with size and timestamp

Hi, Below is a directory containing links new2,list,new1. I need to get the size and timestamp for them. How do i get these details. Please help lrwxrwxrwx 1 xxx abc 11 Nov 24 17:34 new2 -> ./org1/new2 lrwxrwxrwx 1 xxx abc 11 Nov 24 17:34 list -> ./org2/list lrwxrwxrwx 1 xxx abc 10... (2 Replies)
Discussion started by: pradebban
2 Replies

6. Shell Programming and Scripting

Extract date from filename and set timestamp

I have lots of files in this format: dvgrab-2003.06.29_15-30-24.mpg The numbers represents the date and time (YYYY.MM.DD_HH-MM-SS) How can I extract the dates from the filenames, and use the dates in the file timestamp? I guess this can be done by using "find", "sed" and "touch"? Can... (6 Replies)
Discussion started by: qwerty1234
6 Replies

7. Shell Programming and Scripting

Remove timestamp from multiple filename

could you pls help me out in PERL i have a requirement like a.dat.<timestamp> b.dat.<timestamp> c.dat.<timestamp> can you pls help me to rename/remove timestamp a.dat b.dat c.dat Is there any way to remove timestamp alone Thanks (3 Replies)
Discussion started by: vaas
3 Replies

8. Shell Programming and Scripting

Timestamp in the filename

Hi i want to replace the previous time stamp with the current timsatp at the start of the file like 20090710_113354_FT0710a.txt this one to 20091111__113354_FT0710a.txt thanks in advance (3 Replies)
Discussion started by: Reddy482
3 Replies

9. Shell Programming and Scripting

add timestamp to filename

Does anyone know how to add a timestamp to a file's name extension in a shell script? Please help.. (3 Replies)
Discussion started by: walterja
3 Replies

10. UNIX for Dummies Questions & Answers

how to add a timestamp to a filename?

whats going on guys. below is a script i made and am just curious if there is a "time stamp" command. so i can set the timestamp in a filename. #! /bin/ksh # # This scripts takes a list of files in the INDIR variable and compairs it to a list of files that are open in the same directory.... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question