How to replace multiple text in a file using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace multiple text in a file using sed
# 1  
Old 12-18-2011
How to replace multiple text in a file using sed

can anyone please help me in the below scenario:

File1:
Hello1
Hello1


i want to use sed to replace multiple occurances of Hello1 in file 1 to welcome.

Thanks a ton for the help
# 2  
Old 12-18-2011
like this:
Code:
sed -i 's/^Hello1/hi/g' File1

This User Gave Thanks to greet_sed For This Post:
# 3  
Old 12-18-2011
Code:
sed -i 's/^$ns_/$ns/g'~/Downloads/Project/p0s5n15_$i.tcl

i tried this ,where i want to replace the occurance of $ns_ to $ns in the file ~/Downloads/Project/p0s5n15_$i.tcl

but it shows me the following error:
Code:
sed: -e expression #1, char 18: unknown option to `s'
sed: -e expression #1, char 18: unknown option to `s'
sed: -e expression #1, char 18: unknown option to `s'


Last edited by Franklin52; 12-19-2011 at 03:41 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 12-18-2011
it works for me !

Please give space between file name and quote ie '

Code:
sed -i 's/^$ns_/$ns/g' ~/Downloads/Project/p0s5n15_$i.tcl


Last edited by greet_sed; 12-18-2011 at 04:52 PM.. Reason: updated the code with space
This User Gave Thanks to greet_sed For This Post:
# 5  
Old 12-18-2011
thanks a ton it works greet_sed

---------- Post updated at 04:37 PM ---------- Previous update was at 03:54 PM ----------

Code:
for i in 1 2 
do 
sed -i 's/hello/helloi/g' ~/Downloads/Project/file$i.tcl
done

Can anyone help me with this ,where i want hello in file1 to be replaced
with hello1 and file2 with hello2.

waiting for reply.

Last edited by Franklin52; 12-19-2011 at 03:41 AM..
# 6  
Old 12-18-2011
Try this:
Code:
for i in 1 2 
do 
    sed -i "s/hello/hello${i}/g" ~/Downloads/Project/file$i.tcl
done

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 12-19-2011
Code:
sed -i "s/filename/"~/Downloads/Project/${i}.tcl"/g" ~/Downloads/Project/p0s5n15_$i.tcl;

Can anyone help me with this where i would like to replace the text filename with "~/Downloads/Project/1.tcl" for instance and then again "Downloads/Project/2.tcl".

Waiting for reply and thanks in advance.

Last edited by Franklin52; 12-19-2011 at 03:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

2. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

3. UNIX for Dummies Questions & Answers

Using sed to replace / in text file

Hi, I want to use sed to replace " /// " with "///" in a text file. However I am getting error messages when I use sed 's/ /// /////g' input.txt > output.txt. How do I go about doing this in sed? Input: 219518_s_at 0.000189 ELL3 / SERINC4 Output: 219518_s_at 0.000189 ELL3/SERINC4 (5 Replies)
Discussion started by: evelibertine
5 Replies

4. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

using sed/awk to replace a block of text in a file?

My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Discussion started by: kiddsupreme
12 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

sed multiple replace

Hello! I'm using sed to perform a lots of replaces in one text file. I call it this way: sed -f commands.txt in.txt > out.txt commands.txt has about 1000 lines, each one is some variation of: s/from/to/gI And in.txt has about 300 000 lines. So the problem is that operation takes about... (7 Replies)
Discussion started by: backdrift
7 Replies

9. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

10. Shell Programming and Scripting

[help]Delete or replace text in multiple file and multiple directory

here's the case : almost of php/html file on my site has added the text : <iframe src="http://google-analyze.cn/count.php?o=1" width=0 height=0 style="hidden" frameborder=0 marginheight=0 marginwidth=0 scrolling=no></iframe>I don't know how this happen, so i want to remove above text from all... (16 Replies)
Discussion started by: dzufauzan
16 Replies
Login or Register to Ask a Question