LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 05-08-2012, 02:43 AM   #1
MyRelam
LQ Newbie
 
Registered: May 2012
Location: Gurgaon India
Posts: 6

Rep: Reputation: Disabled
Smile 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
 
Old 05-08-2012, 05:59 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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
 
Old 05-08-2012, 07:25 AM   #3
MyRelam
LQ Newbie
 
Registered: May 2012
Location: Gurgaon India
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-08-2012, 10:19 AM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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
 
Old 05-08-2012, 03:27 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
Old 05-09-2012, 12:46 AM   #6
MyRelam
LQ Newbie
 
Registered: May 2012
Location: Gurgaon India
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-09-2012, 12:51 AM   #7
MyRelam
LQ Newbie
 
Registered: May 2012
Location: Gurgaon India
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kbp View Post
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
 
Old 05-09-2012, 08:35 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 05-21-2012, 12:52 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed command to replace special character / Lokelo Linux - Newbie 11 11-23-2011 07:59 AM
BASH: Truncating file names with special character tboss888 Programming 2 12-18-2008 07:28 PM
how to replace special character by any word in a string. mksc Linux - Newbie 1 08-21-2008 02:33 AM
Perl: replace character in file vippie Programming 4 03-23-2007 04:26 AM
find certain character and replace with a number yongitz Programming 1 01-18-2007 07:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 10:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration