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.
I have tried below code but, I am not getting desired result.
My Code -
Mapping files used are -
Above code is giving me this output, which is wrong -
Expected Output is as follows -
Can someone advise on this code or suggest better approach?
Very good that you have shared all your efforts and tried to post nice stuff with complete details. Sorry but it is still NOT fully clear, if you edit your post with more descriptive details and more descriptive sample input and specially sample expected output it will be easy for us to help you on this one. Keep up the good job of learning and sharing, cheers and enjoy learning on this site
Thanks,
R. Singh
Last edited by RavinderSingh13; 04-09-2019 at 09:03 AM..
Very good that you have shared all your efforts and tried to post nice stuff with complete details. Sorry but it is still NOT fully clear, if you edit your post with more descriptive details and more descriptive sample input and specially sample expected output it will be easy for us to help you on this one. Keep up the good job of learning and sharing, cheers and enjoy learning on this site
Thanks,
R. Singh
Thanks Ravinder for your attention.
Basically, I have below pipe delimited record as an input in which column 4 has the filename as received on the source side and column 5 has the filename required as an output. In short, we have to rename filename from format in column 4 to format in column 5. But, here date and time parts are variables in specific format. So, I have created 2 mapping files one with name date_format.csv and other with name timestamp_format.csv which will give predefined formats which are expected in input file.
Input Record -
Expected Output is as follows -
In the above expected output, SOURCE|LOGIN|SERVER|ABC_123_YYYYMMDD_V01P1234YYMMDDHHMISS.txt|ABC_123_V01P.YYYYMMDD.HHMISS.txt|CATEG ORY ---- This part is same as input 9|8|YYYYMMDD|........ ---- 9 is start position of format YYYYMMDD, 8 is length of format YYYYMMDD, YYYYMMDD is date format as mentioned in date_format.csv and ........ is the number of characters in format YYYYMMDD as metioned in date_format.csv. 32|6|HHMISS|...... ---- 32 is start position of format HHMISS, 6 is length of format HHMISS, HHMISS is time format as mentioned in timestamp_format.csv and ...... is the number of characters in format HHMISS as metioned in timestamp_format.csv. 26|6|YYMMDD|...... ---- 26 is start position of format YYMMDD, 6 is length of format YYMMDD, YYMMDD is date format as mentioned in date_format.csv and ...... is the number of characters in format YYMMDD as metioned in date_format.csv. 0|0|| ---- As we do not have second set of HHMISS the results are start_position=0,length=0,format blank (we can keep format as HHMISS|...... if it is easy)
Above part is checked against column 4 and below part is checked against column 5. 14|8|YYYYMMDD|%Y%m%d ---- 14 is start position of format YYYYMMDD, 8 is length of format YYYYMMDD, YYYYMMDD is date format as mentioned in date_format.csv , ........ is the number of characters in format YYYYMMDD as metioned in date_format.csv and %Y%m%d is the date format for YYYYMMDD in date command also defined in date_format.csv. 23|6|HHMISS|......|%H%M%S ---- 23 is start position of format HHMISS, 6 is length of format HHMISS, HHMISS is time format as mentioned in timestamp_format.csv , ...... is the number of characters in format HHMISS as metioned in timestamp_format.csv and %H%M%S is the time format for HHMISS in date command also defined in timestamp_format.csv. 0|0||||0|0|||| ---- remaining parameters are 0 or blank as they represent second occurance of date/time in start_position, length, date/time format, character regex and respective format of date command.
Hope this clarifies the requirement. Let me know if it is still not clear or if you have any better approach to extract date/time part.
This is not easy a request, both to understand and to implement. I have understood that you want to search two input fields, $4 and $5, for date/Ttime format descriptors, and append their metadata to the line. Additional data that shows up in your desired output, as ......, |0|0|, or %Y%m%d are even harder to implement as specified. Try this a a first starting point:
This is not easy a request, both to understand and to implement. I have understood that you want to search two input fields, $4 and $5, for date/Ttime format descriptors, and append their metadata to the line. Additional data that shows up in your desired output, as ......, |0|0|, or %Y%m%d are even harder to implement as specified. Try this a a first starting point:
and see how far it gets you.
Thanks RudiC. This is very close, I will try to build on your solution and see how I can use it.
Only difference is I want same number of "." characters as in YYYYMMDD, YYMMDD or HHMISS.
So, Output will look like this -
Also, Instead of hardcoding YYYYMMDD, YYMMDD and HHMISS in split ("YYYYMMDD|YYMMDD|HHMISS", FMT) can we use separate files to pass them. I am trying to use below files.
So, I am trying to write awk using three files RECORD, date_format.csv and timestamp_format.csv to achieve below output.
Kindly advise how can I use awk on these three files.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)