Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Extract date and time part from filename Post 303033614 by Prathmesh on Tuesday 9th of April 2019 06:16:45 AM
Old 04-09-2019
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 and rename ABC_123_YYYYMMDD_V01P1234YYMMDDHHMISS.txt file in ABC_123_V01P.YYYYMMDD.HHMISS.txt format i.e. If I receive file as ABC_123_20190401_V01P1234190401190242.txt then I should be able to rename it as ABC_123_V01P.20190401.190242.txt.

Code:
$ cat RECORD
SOURCE|LOGIN|SERVER|ABC_123_YYYYMMDD_V01P1234YYMMDDHHMISS.txt|ABC_123_V01P.YYYYMMDD.HHMISS.txt|CATEGORY
$

I have tried below code but, I am not getting desired result.

My Code -
Code:
$ cat timestamp_format.csv | awk -F"," -v OFS="," '{print $1,$2,$3,$4}' | while IFS="," read FILE_TIMESTAMP FILE_TIMESTAMP_PARAM FILE_TIMESTAMP_REGEX EXP_FILE_TIMESTAMP_FORMAT
do
awk -v OFS="|" -v FILE_TIMESTAMP="${FILE_TIMESTAMP}" -v FILE_TIMESTAMP_REGEX="${FILE_TIMESTAMP_REGEX}" -v EXP_FILE_TIMESTAMP_FORMAT="${EXP_FILE_TIMESTAMP_FORMAT}" 'FNR==NR{a[NR]=$1;b[NR]=$3;c[NR]=$4;CNT=NR;next}
{
SOURCE_FILENAME=$4
TARGET_FILENAME=$5
for (i=1;i<=CNT;i++)
{
VAR=a[i];
DATE_FORMAT_REGEX=b[i];
S1=index($4,VAR);
L1=length(VAR);
S_FTP_FILE_TIMESTAMP1=index(SOURCE_FILENAME,FILE_TIMESTAMP);
L_FTP_FILE_TIMESTAMP1=length(FILE_TIMESTAMP);
TEMP_SOURCE_FILENAME=sub(VAR,DATE_FORMAT_REGEX,SOURCE_FILENAME);
S2=index(SOURCE_FILENAME,VAR);
L2=length(VAR);
S_FTP_FILE_TIMESTAMP2=index(SOURCE_FILENAME,FILE_TIMESTAMP);
L_FTP_FILE_TIMESTAMP2=length(FILE_TIMESTAMP);
for (j=1;j<=CNT;j++)
{
DVAR=a[j];
EXP_DATE_FORMAT=c[j];
DS1=index($5,DVAR);
DL1=length(DVAR);
DS_FILE_TIMESTAMP1=index(TARGET_FILENAME,FILE_TIMESTAMP);
DL_FILE_TIMESTAMP1=length(FILE_TIMESTAMP);
TEMP_TARGET_FILENAME=sub(VAR,DATE_FORMAT_REGEX,TARGET_FILENAME);
DS2=index(TARGET_FILENAME,DVAR);
DL2=length(DVAR);
DS_FILE_TIMESTAMP2=index(TARGET_FILENAME,FILE_TIMESTAMP);
DL_FILE_TIMESTAMP2=length(FILE_TIMESTAMP);
if ( S1 > 0 && DS1 > 0 && S_FTP_FILE_TIMESTAMP1 > 0 && DS_FILE_TIMESTAMP1 > 0 )
{
print $0,S1,L1,substr($4,S1,L1),DATE_FORMAT_REGEX,S_FTP_FILE_TIMESTAMP1,L_FTP_FILE_TIMESTAMP1,FILE_TIMESTAMP,FILE_TIMESTAMP_REGEX,S2,L2,substr($4,S2,L2),DATE_FORMAT_REGEX,S_FTP_FILE_TIMESTAMP2,L_FTP_FILE_TIMESTAMP2,FILE_TIMESTAMP,FILE_TIMESTAMP_REGEX,DS1,DL1,substr($5,DS1,DL1),EXP_DATE_FORMAT,DS_FILE_TIMESTAMP1,DL_FILE_TIMESTAMP1,FILE_TIMESTAMP,FILE_TIMESTAMP_REGEX,EXP_FILE_TIMESTAMP_FORMAT,DS2,DL2,substr($5,DS2,DL2),EXP_DATE_FORMAT,DS_FILE_TIMESTAMP2,DL_FILE_TIMESTAMP2,FILE_TIMESTAMP,FILE_TIMESTAMP_REGEX,EXP_FILE_TIMESTAMP_FORMAT
}
}
}
}' FS="," date_format.csv FS="|" RECORD
done
$

Mapping files used are -
Code:
$ cat date_format.csv
YYYYMMDD,[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9],........,%Y%m%d
YYYY-MM-DD,[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9],....-..-..,%Y-%m-%d
YYYY_MM_DD,[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9],...._.._..,%Y_%m_%d
YYMMDD,[0-9][0-9][0-9][0-9][0-9][0-9],......,%y%m%d
YY-MM-DD,[0-9][0-9]-[0-9][0-9]-[0-9][0-9],..-..-..,%y-%m-%d
DD-MMM-YYYY,[0-9][0-9]-[a-z|A-Z][a-z|A-Z][a-z|A-Z]-[0-9][0-9][0-9][0-9],..-...-....,%d-%b-%Y
DD_MMM_YYYY,[0-9][0-9]_[a-z|A-Z][a-z|A-Z][a-z|A-Z]_[0-9][0-9][0-9][0-9],.._..._....,%d_%b_%Y
D-MMM-YYYY,[0-9]-[a-z|A-Z][a-z|A-Z][a-z|A-Z]-[0-9][0-9][0-9][0-9],.-...-....,%e-%b-%Y
D_MMM_YYYY,[0-9]_[a-z|A-Z][a-z|A-Z][a-z|A-Z]_[0-9][0-9][0-9][0-9],._..._....,%e_%b_%Y
$

Code:
$ cat timestamp_format.csv
XXXXXXXXXX,[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9],..........,%s
HHMISS,[0-9][0-9][0-9][0-9][0-9][0-9],......,%H%M%S
$

Above code is giving me this output, which is wrong -
Code:
SOURCE|LOGIN|SERVER|ABC_123_YYYYMMDD_V01P1234YYMMDDHHMISS.txt|ABC_123_V01P.YYYYMMDD.HHMISS.txt|CATEGORY|9|8|YYYYMMDD|........|32|6|HHMISS|......|0|8|ABC_123_|........|32|6|HHMISS|......|14|8|YYYYMMDD|%Y%m%d|23|6|HHMISS|......|%H%M%S|0|8|ABC_123_|%Y%m%d|23|6|HHMISS|......|%H%M%S
SOURCE|LOGIN|SERVER|ABC_123_YYYYMMDD_V01P1234YYMMDDHHMISS.txt|ABC_123_V01P.YYYYMMDD.HHMISS.txt|CATEGORY|9|8|YYYYMMDD|........|32|6|HHMISS|......|0|8|ABC_123_|........|32|6|HHMISS|......|16|6|YYMMDD|%y%m%d|23|6|HHMISS|......|%H%M%S|0|6|ABC_12|%y%m%d|23|6|HHMISS|......|%H%M%S
SOURCE|LOGIN|SERVER|ABC_123_YYYYMMDD_V01P1234YYMMDDHHMISS.txt|ABC_123_V01P.YYYYMMDD.HHMISS.txt|CATEGORY|11|6|YYMMDD|......|32|6|HHMISS|......|0|6|ABC_12|......|32|6|HHMISS|......|14|8|YYYYMMDD|%Y%m%d|23|6|HHMISS|......|%H%M%S|0|8|ABC_123_|%Y%m%d|23|6|HHMISS|......|%H%M%S
SOURCE|LOGIN|SERVER|ABC_123_YYYYMMDD_V01P1234YYMMDDHHMISS.txt|ABC_123_V01P.YYYYMMDD.HHMISS.txt|CATEGORY|11|6|YYMMDD|......|32|6|HHMISS|......|0|6|ABC_12|......|32|6|HHMISS|......|16|6|YYMMDD|%y%m%d|23|6|HHMISS|......|%H%M%S|0|6|ABC_12|%y%m%d|23|6|HHMISS|......|%H%M%S

Expected Output is as follows -
Code:
SOURCE|LOGIN|SERVER|ABC_123_YYYYMMDD_V01P1234YYMMDDHHMISS.txt|ABC_123_V01P.YYYYMMDD.HHMISS.txt|CATEGORY|9|8|YYYYMMDD|........|32|6|HHMISS|......|26|6|YYMMDD|......|0|0|||14|8|YYYYMMDD|%Y%m%d|23|6|HHMISS|......|%H%M%S|0|0||||0|0||||

Can someone advise on this code or suggest better approach?
This User Gave Thanks to Prathmesh For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Insert date/time within a filename

Hi Guys, I need to script the renaming of files as followins: files: firstjd secondjo thirdjv My script needs to insert the date/time infront of the last 2 characters of the filenames above, any ideas greatly received :) the letters before the last 2 characters could change, I'm only... (7 Replies)
Discussion started by: cooperman
7 Replies

2. 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

3. Shell Programming and Scripting

how to update date part with new increment date time

hi experts, my requirement is like this i need to develop a shell script to update date part with new incremental date time in file some 'X' which is kept at some server location incrementing every two hours.as i am new to this scripting i need support from u people,thanx in advance (1 Reply)
Discussion started by: amanmro
1 Replies

4. 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

5. Shell Programming and Scripting

append a filename with system date and time

Hi, There are similar kind of posts, but none seems like working for me. Please correct me if I'm wrong. I need append/rename file abc.txt with file processed date and time like abc_systemdatetimestamp.txt and move it to different folder. for example I have /source/data/abc.txt ... (1 Reply)
Discussion started by: amsn08
1 Replies

6. 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

7. 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

8. Shell Programming and Scripting

Extract a part of a filename containing a particular word

Hi All, Thanks in Advance Shell Script or Perl Script 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... (7 Replies)
Discussion started by: aealexanderraj
7 Replies

9. UNIX for Dummies Questions & Answers

Extract Date part from the filename

Hi All, I have incoming source files abcmmyy.txt I need to extract the mmyy part from the filename and pass that to a variable . I really appreciate your quick response on this. Thanks raj (7 Replies)
Discussion started by: rajeevm
7 Replies

10. 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
All times are GMT -4. The time now is 12:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy