Script to replace numbers by string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to replace numbers by string
# 1  
Old 02-08-2010
Script to replace numbers by string

Hi!

I need the following script:

- All numbers in a filename (0-9) have to be replace by a String ("Zero"-"Nine")
- The script has to go through all the files in the current directory and has to replace the numbers as described above...

I have no idea how to do this...

Thanks!
Michael
# 2  
Old 02-08-2010
Give a precise filename example please, ty.

---------- Post updated at 12:56 PM ---------- Previous update was at 12:30 PM ----------

And if you want to try it on your own - here is an idea/components how you could do it:
  • ls to read directory content
  • while/read to read the directory listing line by line
  • inside the while/read loop parse the filenames with awk for example
# 3  
Old 02-08-2010
Oh, I'm sorry!

Examples:
---------
Car1Something.png --> CarOneSomething.png
Flower6Number_7.png --> FlowerSixNumber_Seven.png
Foo_5_Bar.png --> Foo_Five_Bar.png

I thought about it already, but thanks for the tips! Maybe I will get it by myself...

Thanks!
Michael
# 4  
Old 02-08-2010
Could try something like:

Code:
ls -1| while read LINE;
do
        mv "${LINE}" `echo $LINE| sed 's/1/One/g; s/2/Two/g'`
done

Three, Four, ... you have to add your own Smilie
# 5  
Old 02-08-2010
Uhhh!? That looks easy... Smilie I expected more code....

I have to study it! Smilie

Thanks a lot!

---------- Post updated at 09:19 AM ---------- Previous update was at 07:42 AM ----------

Works great! Thanks!

Tread closed!
# 6  
Old 02-08-2010
I don't know how well-known it is, but there's even a package for some such conversions. I'm not suggesting it for this task, necessarily, but it's fun to know about.

Code:
# number -l 102934
one hundred two thousand nine hundred thirty-four

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace a string with pattern read from a file

I have two files blocks.txt and rules.txt. In blocks.txt i have the following entries Linux1 Linux2 Linux3 ..... Linux10 In rules.txt i have the lines where a filename pattern starts like 'blk-name.*' I want to replace 'blk-name' with the names read from blocks.txt file I tried... (2 Replies)
Discussion started by: Jag02
2 Replies

2. Shell Programming and Scripting

cant figure out the error in this script (adding numbers in a string) using ubantu shell

hii please help me this is the script num=$1 sum=0 while do x=`expr $num % 10` sum=`expr $sum + $x` num=`expr $num / 10` done echo "Summation is $sum" it is giving error pratyush@ubuntu:~$ sh shell.sh 123 shell.sh: 11: 123: not found Summation is 0 (3 Replies)
Discussion started by: Pratyush Sakhle
3 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

Replace string ids with unique numbers

Hello, I have a file with a 1000 ids in the form of strings. I want to replace each id with a unique numbers in the whole file. each id is repeating in all the columns. I know I can use sed command but there are many ids in file which are need to be converted example of input file B752... (4 Replies)
Discussion started by: ryan9011
4 Replies

5. UNIX for Dummies Questions & Answers

How to perform string replace in shell script?

I have value like ABCDEF,BBCCDD in a shell variable, now i would like to have ABQWEF,BBQWDD in the same shell variable. How can i replace the char at position 3&4 with QW in shell script? (3 Replies)
Discussion started by: vel4ever
3 Replies

6. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies

7. Shell Programming and Scripting

Requesting help to replace a string by my bash script

Hello every1, I need help to replace a string in a file by my bash script. Find: log4j.appender.toLogFile.layout.ConversionPattern= %d %5p (%F:%L) - %m%n= %d %5p (%F:%L) - %m%n Replace: log4j.appender.toLogFile.layout.ConversionPattern= %d %5p (%F:%L) - %m%n I tried by sed, but kept... (7 Replies)
Discussion started by: titanic4u
7 Replies

8. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

9. Shell Programming and Scripting

Replace a random string of numbers

Hi Can someone help me with this one? I have string.. (PROC_PROC_ID == 12183) <--PID is dynamic and i want to replace the PID number with whatever PID from /opt/hpws/apache32_2/logs/httpd.pid file. i'm having problem since the PID on the string is dynamic. It may be 2-5 digits or more. ... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

10. Shell Programming and Scripting

String replace perl script error

I have written down a simple perl program to replace a string from a word file. My script does'nt seem to work. Any pointers to make it work will be appreciated. The code is given below. #!/usr/bin/perl while (<>) { open(FILE,$_); while (<FILE>) { s/This/I did it/g; }... (6 Replies)
Discussion started by: MobileUser
6 Replies
Login or Register to Ask a Question