Red Hat This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
05-08-2012, 02:43 AM
|
#1
|
LQ Newbie
Registered: May 2012
Location: Gurgaon India
Posts: 6
Rep:
|
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
|
|
|
05-08-2012, 05:59 AM
|
#2
|
Senior Member
Registered: Aug 2009
Posts: 3,790
|
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
|
|
|
05-08-2012, 07:25 AM
|
#3
|
LQ Newbie
Registered: May 2012
Location: Gurgaon India
Posts: 6
Original Poster
Rep:
|
Thanks a ton KBP , 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. please suggest me on this.
Thanks in advance.
|
|
|
05-08-2012, 10:19 AM
|
#4
|
Senior Member
Registered: Aug 2009
Posts: 3,790
|
Bash variables will not be interpreted inside single quotes, try replacing with double quotes.
Code:
sed -i "s/$var1/$var2/g" $worktofile
The other issue is that you still aren't treating $var1 as regex
|
|
|
05-08-2012, 03:27 PM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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|"
|
|
1 members found this post helpful.
|
05-09-2012, 12:46 AM
|
#6
|
LQ Newbie
Registered: May 2012
Location: Gurgaon India
Posts: 6
Original Poster
Rep:
|
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
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.
|
|
|
05-09-2012, 12:51 AM
|
#7
|
LQ Newbie
Registered: May 2012
Location: Gurgaon India
Posts: 6
Original Poster
Rep:
|
Quote:
Originally Posted by kbp
Bash variables will not be interpreted inside single quotes, try replacing with double quotes.
Code:
sed -i "s/$var1/$var2/g" $worktofile
The other issue is that you still aren't treating $var1 as regex
|
thanks,
while implementing above line to my code I got the below error
sed: -e expression #1, char 35: unknown option to `s'
Please suggest me to get out of this.
Thanks in advance
|
|
|
05-09-2012, 08:35 AM
|
#8
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
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
|
|
|
05-21-2012, 12:52 AM
|
#9
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
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 10:42 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|