LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Red Hat (https://www.linuxquestions.org/questions/red-hat-31/)
-   -   Find and Replace character/special character from the file (https://www.linuxquestions.org/questions/red-hat-31/find-and-replace-character-special-character-from-the-file-943836/)

MyRelam 05-08-2012 02:43 AM

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

kbp 05-08-2012 05:59 AM

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

MyRelam 05-08-2012 07:25 AM

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.

kbp 05-08-2012 10:19 AM

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

David the H. 05-08-2012 03:27 PM

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|"


MyRelam 05-09-2012 12:46 AM

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.

MyRelam 05-09-2012 12:51 AM

Quote:

Originally Posted by kbp (Post 4673481)
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'
:confused:

Please suggest me to get out of this.

Thanks in advance

David the H. 05-09-2012 08:35 AM

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

chrism01 05-21-2012 12:52 AM

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.