Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing a fiel in perl

Status
Not open for further replies.

MC007

Technical User
Apr 8, 2008
1
GB
I have written a script using sed to edit. Unfortunately the sed version is old and i have to edit file to another one, then copy over it. So i thought id give perl ago. I played around with pie but cannot work out how to pass variables to it and use line numbers.
A basic version of my Script:
#!/usr/bin/ksh
test1()
{
eval sed -e $1 testfile > testfile2
}

test1 "'1s/.*/test /'"
#test1 "'1s/.*/ '$(tput smso)'broken'$(tput rmso)' /'"

So i pass a string to another function, which is read by SED. I have to use line numbers due to duplicate text entries in the text file im editing.
The second test (commented out) does a reverse video to highlight that text.

Can anyone give me pointers as to how i can write the above in perl.
 
I am not sed expert, but if you want to convert the code in perl have a look at following.

Code:
#!/path/to/perl
sub test1
{
 my $para1 = shift ; # take arugument into variable
 eval { `sed -e $para1 testfile > testfile2` ; } ; # run system command using backtick
 #error catching etc.. 
}
 
test1("'1s/.*/test /'") ; # call subroutine test1

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
It's maybe a very minor point, but in fact you would rather not use the backticks. As it says in the Perl Cookbook:
'The backticks are a convenient way to run other programs and gather their output. The backticks do not return until the called program exits. Perl goes to some trouble behind the scenes to collect the output, so it is inefficient to use the backticks and ignore their return value'

Read the section on "Gathering Output from a Program" (chapter 16).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top