Visit Our UNIX and Linux User Community


Bash script to replace a character with another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to replace a character with another
# 1  
Old 12-06-2012
Bash script to replace a character with another

Hi. I'm a complete noob when it comes to scripting. I have approximately 2000 files scattered throughout different locations that I need to rename. The current files have a character, "." , that needs to be replaced with an underscore. I have no clue which route to go about correcting this. Ideally, I could run the script from the root directory and it search & correct the entire drive. Any assistance in the matter is greatly appreciated! Thanks in advance.
# 2  
Old 12-06-2012
Give an example of what the filenames are, is there anything that will identify the filenames?
If you are just looking for files containing a dot, you might run into problems with false positive.

---------- Post updated at 03:25 PM ---------- Previous update was at 03:25 PM ----------

Give an example of what the filenames are, is there anything that will identify the filenames?
If you are just looking for files containing a dot, you might run into problems with false positive.
# 3  
Old 12-06-2012
You can try this

Code:
for i in *.sh; do mv "$i" "${i/.sh}"_sh; done

# 4  
Old 12-06-2012
Quote:
Originally Posted by redhead
Give an example of what the filenames are, is there anything that will identify the filenames?
If you are just looking for files containing a dot, you might run into problems with false positive.

---------- Post updated at 03:25 PM ---------- Previous update was at 03:25 PM ----------

Give an example of what the filenames are, is there anything that will identify the filenames?
If you are just looking for files containing a dot, you might run into problems with false positive.
An example would be: A-12345.123.jpg

These need to be changed from A-12345.123.jpg to A-12345_123.jpg

Thanks.
# 5  
Old 12-06-2012
Try this :
Code:
for i in *.jpeg*
do
first_part=$(echo "${i}" | cut -d. -f1)
second_part=$(echo "${i}" | cut -d. -f2)
mv "$i" "${first_part}_${second_part}"
done


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to replace nth character with comma

I have a requirement as below. In one of my column, I have data which may or may not be separted with coma always. Now I need to validate the length of these text within the coma (if available) and if the length is more than 30 characters, I need to insert a coma either at 30 characters if its... (3 Replies)
Discussion started by: aramacha
3 Replies

2. Shell Programming and Scripting

How to pass enter key or selected character in bash script?

hi, i've bash script thats working... but now i need to add a line....that prompts for user input....like yes and 1 as complete install.... so here's how it looks... $ cd 9200 (cd into directory) $./install (hv to type ./install to run install then ask for) ----do you want to... (4 Replies)
Discussion started by: kernel11
4 Replies

3. Shell Programming and Scripting

Bash replace everything between two character

hi, bash is confusing me. it looks so nice when the script is completed, but the way is sometimes very hard... i want to replace everything between 'P/' and the next '\' with a static string. my regex can occur multiple times in the text. i think i'm close to it.... new='zzzzz'... (4 Replies)
Discussion started by: mohorn
4 Replies

4. Shell Programming and Scripting

bash script search file and insert character when match found

Hi I need a bash script that can search through a text file and when it finds 'FSS1206' I need to put a Letter F 100 spaces after the second instance of FSS1206 The format is the same throughout the file I need to repeat this on every time it finds the second 'FSS1206' in the file I have... (0 Replies)
Discussion started by: firefox2k2
0 Replies

5. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

6. Shell Programming and Scripting

script to replace a character with '(Apostrophe)

Hi Friends, I need a sed or awk based script to replace a character(alphabet) with ' (Apostrophe). I tried to use the following two commands 1) sed -e 's/a/'/' filename 2) awk '{sub("a","'",$1);print}' filename but both got failed. Any help on this. Thanks in advance.. (3 Replies)
Discussion started by: ks_reddy
3 Replies

7. Shell Programming and Scripting

script command to replace character

Hi, i have log like this : Actually i want to change the time stamp, and the rule is like this : and My script is like this : I know the script will loop and loop again after 07:00 like this: Can somebody help me ?? Thanks in advance.. (5 Replies)
Discussion started by: justbow
5 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. Shell Programming and Scripting

bash script to check the first character in string

Hello would appreciate if somebody can post a bash script that checks if the first character of the given string is equal to, say, "a" thnx in advance (2 Replies)
Discussion started by: ole111
2 Replies

10. UNIX for Dummies Questions & Answers

How to Replace,Sort,and Append Character one script

Hi all i am very new to shell scripting,hope u guys can help i need to replace,sort and append character for the file that look like this: 1007032811010001000100000001X700026930409 1007032811010001000200000002X700026930409 1007032711020001000300000003X700026930409... (2 Replies)
Discussion started by: ashikin_8119
2 Replies

Featured Tech Videos