Command to replace character


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Command to replace character
# 1  
Old 10-11-2005
Command to replace character

I've been googling for the following for the past few weeks several times, but haven't yet come across something that I could easily grasp. Can someone point me in the right direction please?

I'm trying to replace a character in file names, i.e. the character is a period, and I want to replace it with nothing. So, if a file name is file.name, I want to be able to execute a command from the shell that will change it to filename. And if this can be scripted so that whenever the script is executed, it'll take out the periods in file names in whatever directory is specified, even better.

Please point me towards whatever resources that will help me figure this out.
# 2  
Old 10-11-2005
Here is basic script.

Code:
#! /bin/sh
# mv.sh
FILE1=file1.ext
FILE2=file2.ext

for file in $FILE1 $FILE2
do
        FILE=${file/\./ }
        echo $file -- $FILE
done

Code:
[~/temp]$ ./mv.sh 
file1.ext -- file1 ext
file2.ext -- file2 ext

You can use this a starting point.

Vino
# 3  
Old 10-13-2005
Can you explain the different components of the script? I'm trying to make sense of it (interpreting the script's contents to English) but I'm having trouble understanding it. Also, what are the differences of the code section from the script section?
# 4  
Old 10-14-2005
Hi,

Try this code. This will get the directory name as the parameter and rename all the filenames with "." in it. I hope the script will be easy for you to understand.

Code:
 
#!/bin/ksh
#Rename the filenames with . in it
if [[ $# != 1 ]]
then
    print "Full Directory path is required"
    exit 1
fi
for i in `ls $1`
do
    newfilename=`print $i | sed 's/\.//g'`
    mv $1/$i $1/$newfilename
done

-Mons
# 5  
Old 10-14-2005
Thanks! I copied and pasted the contents of the script and tested it out with several d*o*t* files I touched, and had fun with it. Thanks!

I do have several questions though ...
1) Can the shell that's used be replaced as sh? I am not sure if the server that has the Unix operating system on has the ksh, although I figure it should. If it can be replaced, will any of the contents have to be modified?

2) Although I have questions about lines 3, 6 - 8, and 10 - 11, 1 being the #!/.../ line, (that's like 99.% of the script's contents ... Smilie ) can you clarify your statement "This will get the directory name as the parameter and rename all the filenames with "." in it."? Does it mean that the directory specified will be the criteria in which the script executes its magic on?

I will be manning commands like fi and sed among others that is included in the script to be able to interpret it to English so I can understand it (that seems to be the only way I can comprehend the Unix commands). Whatever summaries you can provide though would help expediate this process though! Smilie Just kidding, of course, kind of.

Again, thanks, many times over!
# 6  
Old 10-17-2005
Quote:
Originally Posted by HLee1981
Can you explain the different components of the script? I'm trying to make sense of it (interpreting the script's contents to English) but I'm having trouble understanding it. Also, what are the differences of the code section from the script section?
The main line that you need to know is the one in bold.

Code:
FILE1=file1.ext
FILE2=file2.ext

for file in $FILE1 $FILE2
do
        FILE=${file/\./ }
        echo $file -- $FILE
done

file contains the value file1.ext and file2.ext held one at a time. For each value replace the . with a space

And then move the file as 'mv $file $FILE'

${file/\./ } is a shell builtin for the sed command sed 's/\./ /g'

Read through the man pages of sh.

Vino
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

2. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

3. Shell Programming and Scripting

sed command to replace a character at last

Hi All, I have a file having one line only. It is like trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037| I want to replace the numbers in last two columns by As. It should look like trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA| Please, suggest me any shell... (12 Replies)
Discussion started by: mukeshbaranwal
12 Replies

4. Shell Programming and Scripting

Replace '$' with no character using tr command

Hi All, Need your help urgently. since '$' comes at the end of the line i want to remove '$' using tr command: file: 111111|.11|.00$ 222222|2.22|.00$ 333333|.33|.00$ 444444|.44|.00$ Thanks in Advance (7 Replies)
Discussion started by: HemaV
7 Replies

5. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

6. 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

7. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

8. Shell Programming and Scripting

Please help replace character command

hi i have log : i want remove some char become like this: anybody can help me ? (7 Replies)
Discussion started by: justbow
7 Replies

9. 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

10. 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
Login or Register to Ask a Question