LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   replace some characters in the lines (https://www.linuxquestions.org/questions/programming-9/replace-some-characters-in-the-lines-820886/)

liuqiong7618 07-19-2010 10:52 PM

replace some characters in the lines
 
sorry

Sergei Steshenko 07-19-2010 11:01 PM

Quote:

Originally Posted by liuqiong7618 (Post 4038943)
hello:
I want to open the document,find the line including "disable = yes",
then replace the word "yes" to "no"
the purl language is new to me,hope to get your help:
The programming as following, but it failed:

#open the securetty
if (!open(FILE,"/etc/securetty"))
{
print "The file can't be found";
exit;
}
while ($line = <>)
{
chomp($line);
printf $line;
# fine the line that includes "disable = yes"
line1 ='"$line" | grep "disable = yes"';


if [-n "$line1" ]
then
# replace the word "yes" to "no"
sed 's/yes/no/';
else
then print "The string cannot be found";
}

Use CODE tags (go to advanced menu and look for '#' button).

The language is called Perl.

Now, based on what Perl documentation did you write


Code:

        line1 ='"$line" | grep "disable = yes"';
...
        if [-n "$line1" ]
...
            sed 's/yes/no/';

?

I suggest to first read

perldoc perlop
perldoc perlfunc
perldoc perlretut
.

You can also find Perl documentation online: http://perldoc.perl.org/ .

You are trying to write Perl code not understanding very basic things about Perl, like regular expressions, for example. You do no need to call 'sed' or 'grep' from Perl programs.

ghostdog74 07-19-2010 11:39 PM

Code:

sed 's/disable[ \t]*=[ \t]*yes/disable = no/' file

grail 07-20-2010 12:21 AM

hmmmm ... does "sorry" mean this question, of which there is no longer one, is over?

liuqiong7618 07-20-2010 01:28 AM

grail:
"sorry" mean "I am ashamed for the low mistake"
when the programming run: line1 ='"$line" | grep "disable = yes"';
the error message: cann't modify constant item in scalar assignment..near '"$line" | grep "disable = yes"';
can you tell me why?

grail 07-20-2010 02:21 AM

Quote:

"sorry" mean "I am ashamed for the low mistake"
Never let this be the case. If some of us didn't ask questions that later we think are silly then others wouldn't learn either.
I can tell you I have asked some great ones when my brain has been switched off ;)

As for your issue, Sergei is one of the Perl gurus so I would follow his advice :)


All times are GMT -5. The time now is 10:41 PM.