Find and Replace character/special character from the file
Red HatThis forum is for the discussion of Red Hat Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
As you can see the delimiters used are '/' (slashes), to use a *nix path in the replace section you would need to escape any slashes or use a different delimiter.
You can escape the slashes like this:
Code:
sed -i 's/C\:\\ADEPT Suite Data\\/\/home\/neeraj\/Smoke Test\//g' $filetowork
But personally I prefer to use a different delimiter like this as it's a lot easier to read, but you still neeed to escape the back-slashes and special characters as well :
Code:
sed -i 's?C\:\\ADEPT Suite Data\\?/home/neeraj/Smoke Test/?g' $filetowork
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:
var1='C:[\]ADEPT Suite Data[\]'
var2='/home/user/test data/'
sed -i "s|$var1|$var2|"
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|\\|/|'
var1='C:/ADEPT Suite Data'
var2='/home/user/test data'
sed -i "s|$var1|$var2|"
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 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
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[\]'
var2='/home/user/SmokeTest/'
var3='C:[\]Program Files (x86)[\]ADEPT Suite'
var4='/home/neeraj/SmokeTest/Fonts'
sed -i -e "s|$var1|$var2|" -e "s|$var3|$var4|" "$filetowork"
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.