Home: Perl Programming Help: Beginner:
develop the SED command in perl



daragorn_1
New User

Sep 15, 2013, 2:01 PM


Views: 945
develop the SED command in perl

Hello everyone

i have a task and it is to develop the SED command but in perl

Global replacement, match, slash delimiter etc

can you help me with that?

Regards,

Ken


Laurent_R
Veteran / Moderator

Sep 16, 2013, 10:40 AM


Views: 937
Re: [daragorn_1] develop the SED command in perl

The s2p utility already does this. No need to develop it again (unless it is a homework assignment).


daragorn_1
New User

Sep 16, 2013, 12:57 PM


Views: 933
Re: [Laurent_R] develop the SED command in perl

yes it is some kind of homework. can somone help me with it?

Regards,

Dotan


BillKSmith
Veteran

Sep 16, 2013, 3:30 PM


Views: 928
Re: [daragorn_1] develop the SED command in perl

The first thing you should do is write and debug a suite of test cases that will define the scope of your project. The project will be complete when your simulator passes all the tests. Start with the sed manpage. Write at least one test for every feature that you are required to implement. Without this step, you will probably never finish the project and will not know it if you do. Fully executable tests are desirable and will save you time in the long run, but are not necessary. Your first real task is to parse the command line. Choose a module. You do not want to do this yourself. The last thing to do is implement the sed commands. Add them one at a time. Test them with the test you already made. Repeat all the test frequently to make sure that you have not broken something that had been working.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Sep 16, 2013, 3:53 PM


Views: 927
Re: [daragorn_1] develop the SED command in perl


In Reply To
yes it is some kind of homework. can somone help me with it?


Sure, we can help, but we're not gonna do your assignment in your place. Wink

Please show what you have done and where you're having difficulties, we will help you.

If you haven't got anything so far, this is a starter:


Code
sed 's/MA/Massachussetts/' file.txt


will print to the screen a version of file.txt where occurrences of MA have been replaced by the full state name.

Same thing in Perl:


Code
perl -pe 's/MA/Massachussetts/' file.txt


As you can see, it is very similar, but of course, it will not always be that easy. Actually, a Perl one-liner will not be sufficient, you will have to write an actual Perl program. But the Perl s/// substitution operator will definitely have to be used heavily in your progtram.