How to add a datetime stamp at a particular position in a filename?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add a datetime stamp at a particular position in a filename?
# 1  
Old 11-16-2013
How to add a datetime stamp at a particular position in a filename?

hi,

i have some files in a directory say
Code:
abc.txt
def.txt
ghi.txt

i am storing these file names in a temp file.
Code:
ls -l | grep "^-" | awk '{print $NF}' > temp_file$$

i want to add a date time stamp at a particular place in the file names.
it can be
1) before the extension
filename_dateTime.extn (abc_datetime.txt)
2) after the extension
filename.extn_datetime (abc.txt_datetime)

there is a variable which contains the place where the date time stamp needs to be added.

stamp_pos which can contain two possible values
Code:
filename_DT.extn 
filename.extn_DT

i want to parse this string "filename_DT.extn" , and depending on the place where the substring "DT" occurs, i need to add the datetime stamp.

initially i thought, i will 1st read each file name from the temp file(containing list of files in the directory) and also parse the variable stamp_pos and accordingly add substring "DT" to the original file names
suppose,
Code:
stamp_pos="filename_DT.extn"

then the DT will be added as
Code:
abc_DT.txt
def_DT.txt
ghi_DT.txt

in the next step i thought i will parse the each original file name and replace DT with the date time stamp.

do anyone has an idea how to do this?
Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags for multi-line code, sample input, and sample output.

Last edited by Don Cragun; 11-16-2013 at 12:50 PM.. Reason: Fix CODE and ICODE tags.
# 2  
Old 11-16-2013
At some point a toggle must be switched for prefix or suffix.

Let's use a command line argument:
e.i stamp_it 0

Code:
for f in *.txt; do
    case ${1} in
    0)
    echo $(date "+%F").${f} ;;
    1)
    echo ${f}.$(date "+%F") ;;
    *)
    echo ${f} ;;
    esac
done

Why not to solve it as a boolean?

If "prefix" is not an argument at command line do "suffix". You get the idea.

Code:
for f in *.txt; do
    [[ ${1} = "prefix" ]] && echo $(date "+%F").${f} || echo ${f}.$(date "+%F")
done

This User Gave Thanks to Aia For This Post:
# 3  
Old 11-16-2013
Try this and adapt to your needs:
Code:
stamp_pos=filename.extn_DT
[ "${stamp_pos#*_DT}" ]; EC=$?; read DT$EC DT$((1-EC)) <<< "_2013/11/16 "
while IFS=. read FN EXT; do echo $FN$DT0.$EXT$DT1; done <file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add current time stamp column in existing csv file

Hi , I want to add a new column 'current_time stamp' in my existing csv file with current time stamp for all the records.I tried something this but this is printing 0 with date & time and printed date one line above header.Please help awk -F "," 'BEGIN{ OFS="," } {$6=system("date... (5 Replies)
Discussion started by: netdbaind
5 Replies

2. Shell Programming and Scripting

Filename with datetime

Hello All, I need unix command to generate a file with datetime in it. For example : ABC_YYYYMMDDHH24MISS.txt Regards Biswajeet Ghosh (1 Reply)
Discussion started by: bghosh
1 Replies

3. Shell Programming and Scripting

Identify the First Column Position in Second Column and add the position value

Identify the First Column Position in Second Column and add the position value in 3rd column. Sample data: a|c b|d c|a d|b e|e f|g g|f |h |i Expected Output: a|c|1 b|d|2 c|a|3 d|b|4 (6 Replies)
Discussion started by: BrahmaNaiduA
6 Replies

4. Shell Programming and Scripting

How to add the dat time stamp just before the last extension of a file?

hi, A file name is in the form inside a variable FILE_NAME="s1.txt.xls" i want to append date and time stamp inbetween s1.txt and .xls. so after appending date time stamp the file name would be MOD_FILE_NAME="s1.txt.201307251422.xls" currently i am using the below code... (4 Replies)
Discussion started by: Little
4 Replies

5. Shell Programming and Scripting

Add '|' to data file on particular position

I have below scenario where i am getting a flat file but with some bad data incoming data format id|name|ind|crncy 123|xxx|y|usd 234|yy|n| 456|a|y90.5|vvv|gbp ----bad dataneed to cleanse the bad data above by adding a pipe '|' after 3rd column 'ind' if pipe '|' is not already there ... (1 Reply)
Discussion started by: rakesh5300
1 Replies

6. Shell Programming and Scripting

add string and time stamp on each line of file

I have file A.txt A 1023 B 123 C 1223 I want output Hello_12PM_A 1023 Hello_12PM_B 123 Helll_12PM_C 1223 Add Hello and time stamp in AM and PM. (4 Replies)
Discussion started by: asavaliya
4 Replies

7. Shell Programming and Scripting

Need to Add date on first position.Plz help

We have a file as shown below with Header and details below Header.Here Header indicates date field. i Want to add date i.e 20100928 as a first position as shown below without header.Please help us using unix script Source file: H20100928 D04041201609360677PC790010384I ... (17 Replies)
Discussion started by: dprakash
17 Replies

8. Shell Programming and Scripting

Delete Files Based on Datetime Stamp

I have a Unix directory, let's call it /home/id for example purposes. It contains the following files: oldfile.txt.20091101, oldfile.txt.20091102, oldfile.txt.20091103, etc. I am trying to create a Korn Shell script that will go to /home/id and delete any oldfile.txt that has a datetime stamp... (1 Reply)
Discussion started by: ijmoore
1 Replies

9. Shell Programming and Scripting

Add minutes in Datetime Variable in Unix

Hi, New to scripting. I have datetime as a String and I need to add some hours/minutes in to that. 21/10/2009 23:00:00 After adding 180 minutes; it will look like this 22/10/2009 02:00:00 Regards, Vikas Mahajan (9 Replies)
Discussion started by: vikas482
9 Replies

10. Shell Programming and Scripting

How to add a column numbers at a particular position?

Problem discription: I have many files which contain the same lines. for instance, (15 lines) file1 ..last column add by hand arbitrarily. 1.78116800 0.68396600 0.00061900 0.47641600 -0.49794500 -0.00024000 -1.70662800 0.29577100 0.67863600 -1.70647600 0.29654600 ... (9 Replies)
Discussion started by: liuzhencc
9 Replies
Login or Register to Ask a Question