problem in replacing a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem in replacing a string
# 8  
Old 05-02-2008
Lightbulb

Hi,

To make it clear, My output file will contain the path of the directory present somewhere in it. I am assigning this path to a variable because the path might keep changing. That is i am using a diff command to check if any additional files are present in any of the two directories i am comparing. The command displays in my output the additional file present in it as
only in a/b/c/d File1.txt.
Where a/b/c/d is the path. I dont want to expose the path in my output file.Instead i need to replace it with the input which i accept from the user.I am adding few more strings to the user input and assigning to another variable. So wherever i find the path in my output file i need to replace it with the modified user input.

assuming my variable are var1 and var2. When i tried
sed -e "s#$var1#$var2#" filename it didnt work.


2nd question

sed -e "s#$variable#sbcd#" filename

This command replaces $variable with the sring sbcd. What if i need to replace sbcd with the variable?

sed -e "s#sbcd#$variable#" filename - is not working.

Is it clear now?

Last edited by ragavhere; 05-02-2008 at 09:07 AM.. Reason: made the question clear
# 9  
Old 05-02-2008
Java

Quote:
Originally Posted by ragavhere
Hi,

To make it clear, My output file will contain the path of the directory present somewhere in it. I am assigning this path to a variable because the path might keep changing. That is i am using a diff command to check if any additional files are present in any of the two directories i am comparing. The command displays in my output the additional file present in it as
only in a/b/c/d File1.txt.
Where a/b/c/d is the path. I dont want to expose the path in my output file.Instead i need to replace it with the input which i accept from the user.I am adding few more strings to the user input and assigning to another variable. So wherever i find the path in my output file i need to replace it with the modified user input.

assuming my variable are var1 and var2. When i tried
sed -e "s#$var1#$var2#" filename it didnt work.


2nd question

sed -e "s#$variable#sbcd#" filename

This command replaces $variable with the sring sbcd. What if i need to replace sbcd with the variable?

sed -e "s#sbcd#$variable#" filename - is not working.

Is it clear now?
Based on my understanding,Let us assume the directory user inputs is in the variable $USERIN.Now you want to do filter the output file so as to replace the exact directory path with the one user provides/inputs.

Code:
sed -e s#path_to_replace#$USERIN# file.txt

But the above code doesn't modify the file,it just extracts the contents and display it to you.If you want to alter the file please do this :

Code:
sed -e s#path_to_replace#$USERIN# file.txt > file1.txt
mv file1.txt file.txt

"The redirection symbol ">" directs the output from sed to the file newfile. Don't redirect the output from the command back to the input file or you will overwrite the input file. This will happen before sed even gets a chance to process the file, effectively destroying your data."
# 10  
Old 05-02-2008
Power problem in replacing a string

Hi all,

Thanks for your help.

I used

sed "s#$var1#$var2#g" file.txt

It worked. It solved my requirements.and to replace a string with a variable i used
sed "s#string#$var#g" file.txt

This code is also working fine

sed -e s#path_to_replace#$USERIN# file.txt.

Thanks DILEEP410 .

Last edited by ragavhere; 05-02-2008 at 10:08 AM.. Reason: Other code working fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a string

Hi All, I have a many folders in a directory under which there are many subdirectories containing text files containing the word "shyam" in them.I want all the files in all the directories containing "shyam to "ram" ?? sed "s/shyam/ram/g" does it ??But anyone can help me with the script ?? ... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

2. Shell Programming and Scripting

Help with replacing string

Hi All, I have below requirement: I need to read each line in file.txt and replace string starting from position 9 to 24 {111111111111111,222222222222222,333333333333333} by common string "444444444444444" and save file. File.txt: 03000003111111111111111 ... (3 Replies)
Discussion started by: smalode
3 Replies

3. UNIX for Dummies Questions & Answers

replacing a string with another string in a txt file

Dear all, I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot! 8 70003200 21.6206 9 70005700 17.5064 10 70002200 . 11 70005100 19.1001 17 70008000 16.1970 32 70012400 26.3465 33... (9 Replies)
Discussion started by: forevertl
9 Replies

4. Shell Programming and Scripting

Help replacing string

Help! I'm trying this command but keep getting illegal syntax etc. awk '{ sub(/00012345/,"000123456"); print}' >newfile I don't understand. It works on one unix machine but not another! (4 Replies)
Discussion started by: Grueben
4 Replies

5. Shell Programming and Scripting

Replacing a string with a space

I'm trying to replace a string "99999999'" with the blank where ever is there in the file. Could you please help in unix scripting. Thank You. (6 Replies)
Discussion started by: vsairam
6 Replies

6. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies

7. Shell Programming and Scripting

problem with sed while replacing a string with another

Hi, I have a line something like this sys,systematic,system I want to replace only the word system with HI I used sed for this as below echo sys,systematic,system | sed 's/system/HI/' but I got output as sys,HIatic,system I wanted output as sys,systematic,HI Please tell me... (9 Replies)
Discussion started by: friendyboy
9 Replies

8. UNIX for Dummies Questions & Answers

Replacing string

Hi there, I'd like to replace STRING_ZERO in FILE_ZERO.txt with the value of VALUEi-th by using something like that: VALUE1=1000 VALUE2=2000 VALUE3=3000 for((i=1;i<=3;i++)); do sed "s/STRING_ZERO/$VALUE'$i'/" FILE_ZERO.txt >> FILE_NEW.txt; done but it doesn't work... Any help... (9 Replies)
Discussion started by: Giordano Bruno
9 Replies

9. Shell Programming and Scripting

Problem replacing the string

I have three files that the string inside it I want to replace so my code will be #!/bin/bash read -p "please input the old string:" string1 read -p "please input the new string:" string2 sed -i "s/string1/string2/g" *.c but the problem is.. the string that I want to replace can't be... (2 Replies)
Discussion started by: Viken
2 Replies

10. Shell Programming and Scripting

string replacing

hii, i need a unix command which replaces all occurrences of a substring within a string with another substring. My solution: string="plalstalplal" sub1="al" sub2="mlkl" echo sed 's/$s2/$s3/g' < s1 > p i want to know how to read the variables s2 and s3.. thaks a lot bye (1 Reply)
Discussion started by: priya_9patil
1 Replies
Login or Register to Ask a Question