Replacing a word with another in shell programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a word with another in shell programming
# 1  
Old 05-08-2008
Replacing a word with another in shell programming

Hello everyone,

I am trying to replace a word by another using shell programming.

for that, I can use

sed -i 's/second_word/first_word/' file_name

but for me,the problem is that I don't know one of the words, It is present in a variable that are created dynamically by the script.

for example

I have a line in the file

127.1.1.1 root localhost localcomp

Here root is my hostname

I want to replace the hostname 'root' with a different hostname.

Script will ask the user to provide the hostname and keeps it in a variable '$hostname'

I want to replace this 'root' with '$hostname'

Can anyone help me with this.

Thanx in advance..

--Sunny

Last edited by eamani_sun; 06-12-2008 at 09:48 PM..
# 2  
Old 05-08-2008
One suggestion. Create a skeleton script that you build the new script from.

give your line this format

127.1.1.1 <hostname> localhost localcomp

Replace this variable with what is entered by the user and write to a new file

sed -e "s/<hostname>/$hostname/" < skeleton.file > updated.file
# 3  
Old 05-08-2008
I have tried that, the problem is that the replacement function

s/<hostname>/$hostname/


It is not taking the value of the variable, instead it is printing the '$hostname' in the output file.

Is there anyway I can avoid that.

--Sundeep
# 4  
Old 05-08-2008
I just tried the following and works like a charm...

Code:
sed "s/root/$hostname/" input.txt

or

sed "s/root/$hostname/" input.txt > output.txt

Make sure the its the "double quotes"

'single tick' will not resolve the $hostname
# 5  
Old 05-08-2008
Thanx a lot hemang. I appreciate your help.

--Sundeep
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing the first word if the word three match

Dear ALL, I have sample file : IDcentos-forum,bash,linuxCentOS,GNome IEfedora-milis,cli,linuxRedhat,KDE IRfreebsd-milis,aix,unixbsd,pyton required output: centos,bash,linuxCentOS,GNome fedora,cli,linuxRedhat,KDE freebsd,aix,unixbsd,pyton Can you help me pls.. (1 Reply)
Discussion started by: gnulyn
1 Replies

2. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

3. UNIX for Dummies Questions & Answers

Replacing word and Capitalize words after

I have an assignment and I am not sure what to do. In Unix, I use PuTTY change the semicolon (;) to a period, and capitalize the first letter of the word immediately after it. I know change command is M-% and "." so only one semicolon is changed but I am not sure how to... (1 Reply)
Discussion started by: kathrut43
1 Replies

4. Shell Programming and Scripting

Replacing first word while extracting

Hello All, I am extracting a part of file. the file looks as follows USING CHARACTER SET UTF8 DEFINE JOB ( DEFINE SCHEMA Flat_File_Schema ( cntnt_id VARCHAR(10) ); DEFINE OPERATOR o_mload TYPE update SCHEMA * ATTRIBUTES ( VARCHAR TdpId = @TdpId (5 Replies)
Discussion started by: nnani
5 Replies

5. Shell Programming and Scripting

replacing multiple word in unix

Dear all, I am new to unix and i have some problem over here Is there any way to replace few words in a text file using sed command? Let say i want to replace "I into He" "love into hate" and "you into her" I was trying this out but it does not seems to work sed { 's/I/He/g' ;... (3 Replies)
Discussion started by: branred
3 Replies

6. Shell Programming and Scripting

Replacing a word with a buch of lines

Input file: I want to replace "change" with the below lines What is the best way to replace it? (5 Replies)
Discussion started by: gomes1333
5 Replies

7. Shell Programming and Scripting

Replacing a word

Dear Friends, I am facing some problem with some hundred files. Hence to use these file I have to make a small change in each file. But its not possible to open each file and make changes manually. Hence I need guidance frm you to automate it. Example File name is "Queue_11.txt" reload sec=F... (6 Replies)
Discussion started by: anushree.a
6 Replies

8. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 Replies

9. Shell Programming and Scripting

Replacing word and Incrementing

Hi I'm having difficulty in writing a script with searching a specified word using sed and replaces that word with numbers that is incremented I tried this: #!/bin/sh awk '{ for (i=2010; i<=NF; i++) sed 's/TBA/$i; }' filename.txt > outputfile.txt but it doesn't work. here is my desired... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

10. Shell Programming and Scripting

Replacing of word

Hi all, I wanted to replace one word with another word pl help me to solve the same. example:- I wanted to replace RXOTX with RXOTRX in a perticuler file with hole. Regards, Ramesh (2 Replies)
Discussion started by: Ramesh Vellanki
2 Replies
Login or Register to Ask a Question