How to remove numbers from filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove numbers from filename
# 1  
Old 05-07-2009
How to remove numbers from filename

Hi all,

Can I edit this script:

Code:
find . -type f | while read i;do [ "$i" != "${i//abc/}" ] && mv "$i" "${i//abc/}" ;done

so that it will not only take out abc from the filename but also take out any numbers [Date, as in the year i.e. 2009 or any other year] that might be in the filename as well.

An example would be,

HTML Code:
Input:
filename abc 2009.mov

Output:
filename.mov
or

HTML Code:
Input:
filename.abc.2009.mov

Output:
filename.mov

Regards Smilie

Last edited by Monkey Dean; 05-07-2009 at 06:52 AM..
# 2  
Old 05-07-2009
use

Code:
sed 's/\(^[aA-zZ]*\).*\(\.[aA-zZ]*$\)/\1\2/g'


cheers,
Devaraj Takhellambam
# 3  
Old 05-07-2009
In "${i//abc/}" abc is regarded as regular expression (regexp).
You need only to improve on the regexp to make it match what you want.
Regexp syntax for bash and ksh is a little different from the POSIX syntax (i.e. egrep).
  • abc matches just "abc" on any position
  • abc+([0-9]) matches "abc", followed by one or more decimal digits, on any position
  • abc *+([0-9]) matches "abc", followed by zero or more spaces, followed by one or more decimal digits, on any position
  • abc *+([0-9])$ matches "abc", followed by zero or more spaces, followed by one or more decimal digits, only if at the end of the filename

If you need to remove "abc" and the number, keeping what is in between, then you have to apply two successive ${i//.../} operations; alternatively you can use backreferences like:
Code:
${i//removethis1\(keepthis1\)removethis2/\1}

where keepthis1 and removethis1...removethis2 are regexp.

Well, I don't remember if bash or ksh93 really do support backreferences.

Last edited by colemar; 05-09-2009 at 09:27 AM..
# 4  
Old 05-09-2009
Colmar THAT'S AWESOME!!! THANK YOU FOR NOT ONLY GIVING ME THE SCRIPT I NEEDED BUT EXPLAINING THE WHOLE THING AS WELL.

Thanks Again. SmilieSmilieSmilieSmilieSmilieSmilie
# 5  
Old 05-09-2009
Quote:
Originally Posted by colemar
In "${i//abc/}" abc is regarded as regular expression (regexp).

No, it is a file globbing pattern not a regular expression.
# 6  
Old 05-11-2009
Quote:
Originally Posted by cfajohnson
No, it is a file globbing pattern not a regular expression.
Perhaps it is not a regular expression as formally defined in the computation theory, but it has almost the same power as the POSIX regexps have.

In the "turtle book" this syntax is referred as "regular expression":
[Chapter 4] 4.3 String Operators

It can even be the case that ${var//pattern/replace} has support for more metacharacters, because this syntax is found in ksh93 while the above book is about ksh88.

I believe a globbing pattern is one that has only two metacharacters, commonly known as "wildcards": * and ?

Last edited by colemar; 05-11-2009 at 06:22 PM..
# 7  
Old 05-11-2009
Quote:
Originally Posted by colemar
Perhaps it is not a regular expression as formally defined in the computation theory, but it has the same power as the POSIX regexps have.

Extended globbing (as implemented in ksh and with the extglob option in bash) adds some very limited regular expression capabilities with a completely different syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove spaces in filename

Hi team, Here's a requirement for me. Here are the list of files i have in a unix directory. W 2 A D_2014.csv W 3 A D_2014.csv W 4 A D_2014.csv /home/kmani00-> uname -a AIX sliyyvxx 1 6 00F613E54C00 /home/kmani00-> The file names has to be without spaces as follows. W2AD_2014.csv... (1 Reply)
Discussion started by: kmanivan82
1 Replies

2. Shell Programming and Scripting

Remove the last 9 characters of a filename

Hi All! Please can someone help, I have a dir with the following files: ~-rw-r--r-- 1 emmuser users 2087361 Oct 16 15:50 MPGGSN02_20131007234519_24291.20131007 -rw-r--r-- 1 emmuser users 2086837 Oct 16 15:50 MPGGSN02_20131007233529_24272.20131007 -rw-r--r-- 1 emmuser ... (7 Replies)
Discussion started by: fretagi
7 Replies

3. Shell Programming and Scripting

Renaming file that has multiple numbers as filename

Hi I have a file with filename as "partition-setup-and-ipl.vtcmd.76217657132.9721536798" Now i need to move this file as "partition-setup-and-ipl.vtcmd.76217657132.9721536798_org" i tried with # ls | grep -E "partition-setup-and-ipl.vtcmd.+"... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

increase/decrease multiple numbers in a filename.

I got a game that output map tiles of the session with the 0,0 position at the place you login/spawn. That makes making a map somewhat troublesome since the 0,0 will move. So I've been looking for a way to change the numbers in the filenames of all files in a folder by a certain value. The... (5 Replies)
Discussion started by: Ravenholdt
5 Replies

5. Shell Programming and Scripting

Remove last character from filename

Hi All, I have different type of file (.txt,.csv,.xml) format in my current directory. My requirement is that I need to remove the last character from the file format. Example count.txt$ csp_rules.csv^ Date.xml~ Need Output: count.txt csp_rules.csv Date.xml How to do that?.... (5 Replies)
Discussion started by: suresh01_apk
5 Replies

6. Shell Programming and Scripting

remove the filename from a string

I have a string like this /Development/ST/st000001su/Outbound/Prod/PROD-732QCJ/63acf2caf91bc136cb9bcce8a85c7fa8/PGP/PGP.txt I want to remove the PGP.txt and I want only the /Development/ST/st000001su/Outbound/Prod/MCFR-732QCJ/63acf2caf91bc136cb9bcce8a85c7fa8/PGP returned. I saw an command... (2 Replies)
Discussion started by: srini0603
2 Replies

7. Shell Programming and Scripting

delete numbers in a filename

I have some files where numbers are part of like eg 1add1.txt 23sub41.txt etc I want to remove numbers from the filenames(whereever it may be). I used echo `ls *.txt | sed -e "s///"` But its removing first digits like 1add1.txt becomes add1.txt My intention is to make 1add1.txt... (3 Replies)
Discussion started by: villain41
3 Replies

8. UNIX for Dummies Questions & Answers

Remove path from filename

In a foreach loop I end up with $file containing the filename INCLUDING the whole path. I want this reduced to just the filename, but I can't seem to remember how I did it some years back. I am sure I can do it with "sed", but I am pretty sure I have seen a simpler command. Anyone? borgeh (3 Replies)
Discussion started by: borgeh
3 Replies

9. Shell Programming and Scripting

script to change filename with numbers

ok, this one is definitely too hard for my shell-script-skills. Hopefully, there is somebody who can help me with this: I have a folder with files in it named 0.ppm 10.ppm 2.ppm ... 5.ppm 50.ppm 55.ppm ... 355.ppm 360.ppm etc. As you will notice, the order in which the files are... (5 Replies)
Discussion started by: silversurfer202
5 Replies

10. UNIX for Dummies Questions & Answers

remove filename prefix

I've got a bunch of files called oldabc, olddef etc. i want to copy these to be abc, def.... I can do this with file extensions....but can get the logic to work for prefixes. All the files I am interested in have a prefix of 'old'. This loop is no good for me....it looks at the content... (2 Replies)
Discussion started by: peter.herlihy
2 Replies
Login or Register to Ask a Question