Regular expression, text replacement using simple edit - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Regular expression, text replacement using simple edit

I'm trying to make a shell script that can change class names in source files.

The following will run without an error message but it doesn't do exactly what I want.

[code]sed 's/from/to/g' somefile_containing_from.txt[/code]

That command will replace all occurrences of the word "from" to "to" while printing out the contents of the file. What I need is something where it only matches when there is a non-letter before and after the "from".

Due to the problem in the above command, the following undesired replacements happen:

File contents for before running through sed command:
public class student
private student[] topstudent;

using command:
[code]sed 's/student/Student/g'[/code]

Prints the following:
public class Student
private Student[] topStudent;

Notice that the variable name changed because it found "student" in the output even though topstudent was not the class name.
Sign In or Register to comment.