replace the - with _ in a file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace the - with _ in a file name
# 1  
Old 06-21-2011
CPU & Memory replace the - with _ in a file name

Hello friends,
I have around 1000 files in a directory with different kind of file names like Championships-2009--How-to-get-in-27781, wiki1.jpg,used_shutterstock_2881985_4.jpg,stock-43047295.jpg, corporate-hospitality---19538 etc.

I need a script which can replace only one - (dash) near to digits only from those files names which are ending with only digits.
Like Championships-2009--How-to-get-in-27781 to Championships-2009--How-to-get-in_27781 and corporate-hospitality---19538 to corporate-hospitality--_19538

Please help to solve this.
Thanks in advance.
# 2  
Old 06-21-2011
Code:
 
echo "Championships-2009--How-to-get-in-27781" | sed 's/\(.*\)-\([0-9].*\)/\1_\2/' 
 
echo "corporate-hospitality---19538" | sed 's/\(.*\)-\([0-9].*\)/\1_\2/'

# 3  
Old 06-21-2011
Quote:
Originally Posted by panyam
Code:
 
echo "Championships-2009--How-to-get-in-27781" | sed 's/\(.*\)-\([0-9].*\)/\1_\2/' 
 
echo "corporate-hospitality---19538" | sed 's/\(.*\)-\([0-9].*\)/\1_\2/'


But how do you find all the files with name ending with -[0-9]digit in a directory??

---------- Post updated at 07:01 PM ---------- Previous update was at 03:58 PM ----------

any help ??
# 4  
Old 06-21-2011
Code:
 
ls -l *_[0-9]*

# 5  
Old 06-21-2011
Quote:
Originally Posted by panyam
Code:
 
ls -l *_[0-9]*

That's incorrect. The * allows a string of arbitrary length and of arbitrary characters to follow a digit. It will absolutely not insure that the name matched ends with digits.

Regards,
Alister
# 6  
Old 06-21-2011
CPU & Memory

Quote:
Originally Posted by panyam
Code:
 
ls -l *_[0-9]*

Thanks for reply.
I think you misunderstood my question. I want to replace only one - with _ of file names, like Time-to-Panic--585 . It should change to Time-to-Panic-_5852 and there are more that 1000 files in a directory with various name patterns, like sharp3dphone--2_0.jpeg, Paying-the-piper-6091--big, Z06-Corvette--4471 etc. So i need a script which can find out only those files whose names are ending with -[0-9] pattern and replace only one - with _ it means -[0-9] to _[0-9].

Please try to solve it.
# 7  
Old 06-21-2011
Hello Alister,

Sorry that is my ignorance.

Vikky,

Something like this:

Code:
 
for file in `ls *-[0-9]* | grep -e "-[0-9]*[0-9]$"`
do
echo $file
mv $file `echo $file | sed 's/\(.*\)-\([0-9].*\)/\1_\2/'`
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

2. Shell Programming and Scripting

Avoid overwriting backup file when multiple entries need to replace in one file input from another

Hello, I have been working on script in which search and replace the multiple pattern. 1. update_params.sh read the multiple pattern from input file ParamMapping.txt(old_entry|New_entry) and passing this values one by one to change_text.sh 2. change_text.sh read... (0 Replies)
Discussion started by: ketanraut
0 Replies

3. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

5. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

6. Shell Programming and Scripting

Search and replace with mapping from a mapper file in a target file

Hello, I have a special problem. I have a file in 8 bit and would like to convert the whole database to 16Bit unicode. The mapping file has the following structure: The mapper is provided as a zip file The target file to be converted contains data in English and 8 bit Urdu mapping, a... (4 Replies)
Discussion started by: gimley
4 Replies

7. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

8. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

9. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

10. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies
Login or Register to Ask a Question