Find and Replace character/special character from the file
Hi All, Environment: Linux RedHat 4 Enterprise File type: .ini/.sh/.txt I am working on the script which is run on the RedHat 4 Enterprise edition. I have multiple files in one directory, that file contain multiple below lines Infile=C:\XXXX Yyuyh Data\AFP\bbb\data\hju.plo Resdir=C:\ADEPT Suite Data\AFP\bbb\res\; UsePreciseCharacterAdvance=1 Now this is windows based file which must be get convert as Linux format. below is the line which replace the windows string to Linux Infile=/home/user/test data/AFP\bbb\data\hju.plo Resdir=/home/user/test data/AFP\bbb\res\; UsePreciseCharacterAdvance=1 Now here you can see this .. I need to replace character as well as add some words also. I m using "sed" command but its not working. sed -i 's/C:\ADEPT Suite Data\/home/neeraj/Smoke Test/g' $filetowork Please let me know if you need any other details regarding this. Any help would be highly appreciated :) You can reach me any time through mail niraj06.srivastava@gmail.com Thanks & Regards |
There are a couple of things you need to know, it won't work because you're using the same character in the replace part as the delimiter, and you're not treating the find section as a regex. The normal format for a sed replace might look like:
Code:
sed -i 's/<find_this>/<replace_with_this>/g' $file You can escape the slashes like this: Code:
sed -i 's/C\:\\ADEPT Suite Data\\/\/home\/neeraj\/Smoke Test\//g' $filetowork Code:
sed -i 's?C\:\\ADEPT Suite Data\\?/home/neeraj/Smoke Test/?g' $filetowork |
Thanks a ton KBP:hattip:, it works :)...
Now I am declaring two variable. one variable contain the cold value and another contain the new value.. var1= "C:\ADEPT Suite Data\" var2= "/home/user/test data/" sed -i 's/$var1/$var2/g' $worktofile output: /home/user/test data/AFP\bbb\data\hju.plo later I will change the '/' to '\'. but above code is not working as well. the way i have declared the path into the variable might be there is an issue.:tisk: please suggest me on this. Thanks in advance. |
Bash variables will not be interpreted inside single quotes, try replacing with double quotes.
Code:
sed -i "s/$var1/$var2/g" $worktofile |
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Setting variables uses varname="contents". Note that there are no spaces around the "=" sign. Make sure you quote contents with spaces and other reserved characters. The second problem is with the backslashes in the left hand side of sed, and the forward slashes on the right side. The LHS can be corrected with backslash escapes on the backslashes, as mentioned, but actually I think it's cleaner to use regex bracket expressions instead. The RHS problem is fixed by changing the delimiter, again as discussed (I like using "|" personally. Code:
By the way, a more general solution might be to just convert all backslashes into forward slashes first. Then you can go back and change the other file path strings afterwards as needed. Code:
sed -i 'y|\\|/|' |
Hi David,
Thanks for your response. I tried it but I am facing below error, I tried it out but not yet get any solution for this :confused: sed: -e expression #1, char 0: no previous regular expression Below is my code which I am running. #!/bin/sh echo "Enter filename which need to change: " read filetowork Var1='C:[\]ADEPT Suite Data[\]AFP[\]' Var2='/home/user/SmokeTest/' sed -i "s|$var1|$var2|" $filetowork var3='C:[\]Program Files (x86)[\]ADEPT Suite' var4='/home/neeraj/SmokeTest/Fonts' sed -i "s|$var3|$var4|" $filetowork Please suggest me on this to solve this error. Thanks in advance. |
Quote:
while implementing above line to my code I got the below error sed: -e expression #1, char 35: unknown option to `s' :confused: Please suggest me to get out of this. Thanks in advance |
1)
I asked you to please use [code][/code] tags around your code and data. They are necessary to ensure that everything displays exactly as you entered it, and to keep long lines from interfering with the page layout. 2) Almost everything in Linux/Unix is case-sensitive. $Var and $var are two different variables. 3) Also, sed can apply multiple expressions at once, using "-e" options. Code:
var1='C:[\]ADEPT Suite Data[\]AFP[\]' 4) QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later. http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes 5) It's obvious that you really need to take some time to learn the basics of scripting. As soon as you have time, read through the bash guide here, or one of the many other good tutorials available. http://mywiki.wooledge.org/BashGuide |
Depending on how you are copying this from the MS box, you may need to run dos2unix to replace the line endings http://linux.die.net/man/1/dos2unix
|
All times are GMT -5. The time now is 01:20 PM. |