[Solved] Modifying/Removing Timestamps from a filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Modifying/Removing Timestamps from a filename
# 1  
Old 02-23-2014
[Solved] Modifying/Removing Timestamps from a filename

So given filenames of varying lengths, I was wondering how I would remove or modify appended timestamps of the current date DD-MM-YY.

So say:
Code:
test_DD-MM-YY.txt
coolbeans_DD-MM-YY.pdf

And what I expect the output to be:
Code:
test.txt
coolbeans.pdf

Thanks Smilie

Last edited by Don Cragun; 02-23-2014 at 03:40 AM.. Reason: Add CODE tags.
# 2  
Old 02-23-2014
Perhaps the following examples will help:
Code:
#!/bin/ksh
for infile in test_DD-MM-YY.txt coolbeans_DD-MM-YY.pdf
do      printf "infile:\t\t%s\n" "$infile"
        # Following will work with recent bash and ksh:
        outfile=${infile/_????????/}
        printf "outfile:\t%s\n" "$outfile"
        # Following will work with any POSIX conforming shell:
        Poutfile=${infile%_*}.${infile#*.}
        printf "POSIX outfile:\t%s\n\n" "$Poutfile"
done

which produces the output:
Code:
infile:		test_DD-MM-YY.txt
outfile:	test.txt
POSIX outfile:	test.txt

infile:		coolbeans_DD-MM-YY.pdf
outfile:	coolbeans.pdf
POSIX outfile:	coolbeans.pdf

# 3  
Old 02-23-2014
. It works and I kind of understand how it works now. SmilieThanksSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing duplicate sequences and modifying a text file

Hi. I've tried several different programs to try and solve this problem, but none of them seem to have done exactly what I want (and I need the file in a very specific format). I have a large file of DNA sequences in a multifasta file like this, with around 15 000 genes: ... (2 Replies)
Discussion started by: 4galaxy7
2 Replies

2. Shell Programming and Scripting

Help with modifying a filename

Hello, I have a filename that looks like this: ABC_96_20141031_041133.232468 I need to shorten only the "_2014" part of the filename to simply "_14" so the filename looks like: ABC_96_141031_041133.232468 I have to do a search for this string because there are hundreds of files and... (17 Replies)
Discussion started by: bbbngowc
17 Replies

3. Shell Programming and Scripting

[Solved] Paste with filename

Hi all, please help, I have several folders in a directory that have the same set of files (content different but names same). I want to use the paste command to merge the 5th column of all files named xyz from all directories with the directory name as col header. for dir in * do && paste... (4 Replies)
Discussion started by: gina.lizar
4 Replies

4. Shell Programming and Scripting

removing a range of characters in a filename

hi, I have quite a bunch of files with annoyingly long filenames. I wanted to cut the range of characters from 9-18 and just retain the first 8 characters and the .extension. any suggestion how to do it. thanks much. original filename: 20000105_20000105_20100503.nc.asc output filename:... (4 Replies)
Discussion started by: ida1215
4 Replies

5. Shell Programming and Scripting

Using, modifying and reporting on timestamps to track changes in a filesystem

So, I have a filesystem with an ever growing number of files in it. Multiple users are able to drop new files into this area and I need to know what's changed (a granularity of month by month is ok, more accurately would be better). There are however a few issues, I can't just use the... (8 Replies)
Discussion started by: DeeGee
8 Replies

6. Shell Programming and Scripting

removing the filename extension

Is there an easy way to strip off a filename's extension? For example, here's a filename: blahblahblah.thisisok.thisisnotok I want to get rid of .thisisnotok from the filename, so that what's left is blahblahblah.thisisok Thanks. I have a directory full of filenames that need to be... (5 Replies)
Discussion started by: daflore
5 Replies

7. UNIX for Dummies Questions & Answers

Removing the trailing date from a filename

Hi I have 3 files (say) in a folder as in the example below abc_01012011.csv def_01012011.csv xyz_01012011.csv I need to move these files to a different folder as follows abc.csv def.csv xyz.csv I am trying to put together a script with a for loop which reads the source filenames... (5 Replies)
Discussion started by: bobsn
5 Replies

8. UNIX for Dummies Questions & Answers

Help with Removing extra characters in Filename

Hi, It's my first time here... anyways, I have a simple problem with these filenames. This is probably too easy for you guys: ABC_20101.2A.2010_01 ABD_20103.2E.2010_04 ABE_20107.2R.2010_08 Expected Output: ABC_20101 ABD_20103 ABE_20107 The only pattern available are the ff: 1) All... (9 Replies)
Discussion started by: Joule
9 Replies

9. Shell Programming and Scripting

Removing spaces within Filename

Hello, I have a Folder (myfile) which contain the following files: P$12789865KR +N+01+OM+16102009165416.nu P$M1-508962GD +N+01+ALP+14102009094417.nu Is there a sed command(s) that will loop through this folder and remove the spaces that exists in the filename? Any help would be... (7 Replies)
Discussion started by: Fishn
7 Replies

10. Shell Programming and Scripting

Sort files by Date-timestamps available in filename & pick the sortedfiles one by one

Hi, I am new to Unix shell scripting. Can you please help me with this immediate requirement to code.. The requirement is as given below. In a directory say Y, I have files like this. PP_100000_28062006_122731_746.dat PP_100000_28062006_122731_745.dat PP_100000_28062006_122734_745.dat... (4 Replies)
Discussion started by: Chindhu
4 Replies
Login or Register to Ask a Question