LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2010, 10:52 PM   #1
liuqiong7618
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Rep: Reputation: 0
replace some characters in the lines


sorry

Last edited by liuqiong7618; 07-19-2010 at 11:14 PM.
 
Old 07-19-2010, 11:01 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by liuqiong7618 View Post
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.
 
Old 07-19-2010, 11:39 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
sed 's/disable[ \t]*=[ \t]*yes/disable = no/' file
 
Old 07-20-2010, 12:21 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,623

Rep: Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942
hmmmm ... does "sorry" mean this question, of which there is no longer one, is over?
 
Old 07-20-2010, 01:28 AM   #5
liuqiong7618
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Original Poster
Rep: Reputation: 0
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?

Last edited by liuqiong7618; 07-20-2010 at 01:35 AM.
 
Old 07-20-2010, 02:21 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,623

Rep: Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove and replace characters in a logfile benthad Programming 6 01-15-2009 03:27 PM
Replace Ctrl-M (^M) characters with spaces.... visitnag Linux - Newbie 3 04-16-2008 09:05 AM
How to modify the names of files and replace characters with other characters or symb peter88 Linux - General 2 12-10-2006 03:05 AM
Replace lines in script AMMullan Linux - Software 9 12-06-2003 11:36 PM
replace null characters in a file Philipp Programming 2 09-20-2001 02:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration