LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-25-2008, 06:19 AM   #1
ajidnair
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Rep: Reputation: 0
Question Replace Numeric From File Names


HI All,

is there any possiblity to remove numerics from the starting of a file-name.

for eg : if i have a file named "09itdesc.txt", i want to remove the "09" from the file-name and would need it as "itdesc.txt"

I am using Fedora-8..

Thanks.........

~Destined !!!!!!
 
Old 02-25-2008, 06:39 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
If it's always the first two positions for i="09itdesc.txt" you could 'echo "${i:2}"', else you could 'echo "${i//[0-9]}"'.
 
Old 02-26-2008, 03:50 AM   #3
ajidnair
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Red face Error on Run!!!!!!!

Thanks for your spontaneous reply "unSpawn", but i get no results if i run the same..


imagine i have a folder docs, inside that i have n number of files. some files will be of the case
09itdesc.txt
10itdesc1.txt
11itdesc2.txt

i would like to relpace "09itdesc.txt" to "itdesc.txt"
Similarly,

10itdesc1.txt => "itdesc1.txt"
11itdesc2.txt => "itdesc2.txt"

i would like to replace the filenames replacing the leading numerics..


Thnks ,
~Destined




Quote:
Originally Posted by unSpawn View Post
If it's always the first two positions for i="09itdesc.txt" you could 'echo "${i:2}"', else you could 'echo "${i//[0-9]}"'.
 
Old 02-26-2008, 04:26 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
It may make more sense to move the digits to the end of the filename:
Code:
for file in *itdesc.txt; do number=$(echo "$file" | sed "s/^\([[:digit:]][[:digit:]]*\)\([[:alpha:]]*\)/\2\1/"); echo $number; done
itdesc01.txt
itdesc02.txt
itdesc03.txt
itdesc04.txt
itdesc05.txt
itdesc06.txt
itdesc07.txt
itdesc08.txt
itdesc09.txt
itdesc10.txt
itdesc11.txt
If this looks OK change echo "${number}" to "mv "${file}" "${number}".

Another option is to use ls *itdesc* | cat -n to produce a list of matching filenames preceded by an ordinal number.
Code:
     1  01itdesc.txt
     2  02itdesc.txt
     3  03itdesc.txt
     4  04itdesc.txt
     5  05itdesc.txt
     6  07itdesc.txt
     7  09itdesc.txt
     8  10itdesc.txt
     9  11itdesc.txt
    10  12itdesc.txt
    11  13itdesc.txt
Look at what characters are used to separate the fields:
ls *itdesc* | cat -n | od -c

Code:
0000000                       1  \t   0   1   i   t   d   e   s   c   .
0000020   t   x   t  \n                       2  \t   0   2   i   t   d
0000040   e   s   c   .   t   x   t  \n                       3  \t   0
...
Note the use of spaces and tabs.
You may want to do this in steps to make it easier to test as you go along, and come up with a sed command the produces a rename script which you can run to perform the renaming. This makes it easier to develop the sed command.
Code:
ls *itdesc* | cat -n >temp
sed 's/^ *\([[:digit:]][[:digit:]]*\)\t\([[:digit:]][[:digit:]]\)\([[:alpha:]]*\)\.\([[:alpha:]]*\)/mv \2\3.\4 \3\1.\4/' temp >renamescript
chmod +x renamescript
rm temp
./renamescript
rm renamescript

Last edited by jschiwal; 02-26-2008 at 04:51 AM.
 
Old 02-26-2008, 04:59 AM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
This strips off the first two characters of the name of all .txt files in a directory:

Code:
for i in *.txt
  do mv $i $(echo $i | cut -c 1-2 --complement)
done
 
Old 02-26-2008, 07:38 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by H_TeXMeX_H View Post
This strips off the first two characters of the name of all .txt files in a directory:

Code:
for i in *.txt
  do mv $i $(echo $i | cut -c 1-2 --complement)
done
Stripping off the first two characters would give rename all of the files in turn to the same name.
"${i#[0-9][0-9]}" would strip them off as well. "${i##*[0-9]}" will strip off any number of digits provided the text part doesn't contain any digits as well.
 
Old 02-27-2008, 11:38 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by jschiwal View Post
Stripping off the first two characters would give rename all of the files in turn to the same name.
In this case:

Quote:
Originally Posted by ajidnair View Post
imagine i have a folder docs, inside that i have n number of files. some files will be of the case
09itdesc.txt
10itdesc1.txt
11itdesc2.txt

i would like to relpace "09itdesc.txt" to "itdesc.txt"
Similarly,

10itdesc1.txt => "itdesc1.txt"
11itdesc2.txt => "itdesc2.txt"

i would like to replace the filenames replacing the leading numerics..
the files would not be renamed the same because they have two sets of numerals: ##itdesc#.txt => itdesc#.txt

But in other cases, yes. However, since the author has this particular case, my solution would work and is perhaps easiest to understand.
 
Old 02-28-2008, 03:00 AM   #8
ajidnair
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Cool Thnkx !!

THanks a lot , it is working well.



~Destined,

P Please don't print this unless you really need to - this will preserve trees on planet earth.
 
Old 02-28-2008, 10:58 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
H_TexMexH:

You are right. I either didn't notice the trailing digit or didn't take them as being literal but rather a way of generalizing the order in the directory listing.
 
  


Reply

Tags
file, rename



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
remove the extra numeric field in a text file powah Programming 13 01-08-2008 08:24 PM
Gnome/Nautilus issues Beagle-Tracker, file transfer info, replace file info, popup. Mysticle31 Linux - Software 0 01-08-2008 05:30 PM
Need to find if a matching file exist from a list of possible file names wit_273 Linux - General 5 10-25-2007 09:47 AM
How to modify the names of files and replace characters with other characters or symb peter88 Linux - General 2 12-10-2006 03:05 AM
how can I replace 1 file on a bootable cd? rblampain Linux - General 3 06-04-2006 08:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration