Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > can substitute and end of string

Reply
Thread Tools

can substitute and end of string

 
 
marcorice@gmail.com
Guest
Posts: n/a
 
      08-03-2005
I'm trying to a substitute in a password and shadow file and perl will
not let me do it for the end of string, can anyone help.

#this works for begin of string.
$line =~ s/^$unixid/$nitd/g;
$line =~ s/\A$unixid/$nitd/g;

#this does not for end of string.
$line =~ s/$unixid$/$ntid/g;
$line =~ s/$unixid\z/$ntid/g;
$line =~ s/$unixid\Z/$ntid/g;

#so therefore this does not for begin and end.
$line =~ s/^$unixid$/$ntid/g;

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-03-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm trying to a substitute in a password and shadow file and perl will
> not let me do it for the end of string, can anyone help.
>
> #this works for begin of string.
> $line =~ s/^$unixid/$nitd/g;
> $line =~ s/\A$unixid/$nitd/g;
>
> #this does not for end of string.
> $line =~ s/$unixid$/$ntid/g;
> $line =~ s/$unixid\z/$ntid/g;
> $line =~ s/$unixid\Z/$ntid/g;


All of those match the "end" (for various definitions of end).
Therefore, I predict that either $line does not contain what you think
it contains, or that $unixid does not contain what you think it
contains.

Please print out both values, demarking the beginning and end:
print "Line: |$line|\n";
print "UnixId: |$unixid|\n";

And see if there is any unexpected whitespace before the terminating |
character.

Paul Lalli

 
Reply With Quote
 
 
 
 
marcorice@gmail.com
Guest
Posts: n/a
 
      08-03-2005
>Please print out both values, demarking the beginning and end:
>print "Line: |$line|\n";
>print "UnixId: |$unixid|\n";

I checked and there are no terminating nor begining whitespace, here's
a little more the code.
# Traverse the UIDs_n_IDs_Not_Match file and Read & Substitute unix ids
w/ NT ids.
foreach (@name_change2) {
my
($NULL1,$unixid,$NULL2,$unixuid,$NULL3,$NULL4,$nti d,$NULL5,$ntuid) =
split /\s*\|\s*/;
$line =~ s/$unixid/$ntid/g;

} #end of foreach

 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      08-03-2005

(E-Mail Removed) wrote:
> >Please print out both values, demarking the beginning and end:
> >print "Line: |$line|\n";
> >print "UnixId: |$unixid|\n";

> I checked and there are no terminating nor begining whitespace,


OK, what about non-printing characters? Got any of those?

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-03-2005
(E-Mail Removed) wrote:
> Paul Lalli wrote:
> >
> >Please print out both values, demarking the beginning and end:
> >print "Line: |$line|\n";
> >print "UnixId: |$unixid|\n";

>
> I checked and there are no terminating nor begining whitespace, here's
> a little more the code.
> # Traverse the UIDs_n_IDs_Not_Match file and Read & Substitute unix ids
> w/ NT ids.
> foreach (@name_change2) {
> my
> ($NULL1,$unixid,$NULL2,$unixuid,$NULL3,$NULL4,$nti d,$NULL5,$ntuid) =
> split /\s*\|\s*/;


You seem to have ignored the advice I gave yesterday about how to clean
this up.

> $line =~ s/$unixid/$ntid/g;
>
> } #end of foreach


There is no attempt to match the end of the string in this code.

Please post a SHORT BUT COMPLETE script that SHOWS THE PROBLEM you are
encountering. Make sure you include statements that will cause the
relevant data to be identified (ie, the aforementioned print
statements). Also include the erroneous output that this short but
complete script generates.

Paul Lalli

 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      08-03-2005
At 2005-08-03 11:53AM, (E-Mail Removed) <(E-Mail Removed)> wrote:
> >Please print out both values, demarking the beginning and end:
> >print "Line: |$line|\n";
> >print "UnixId: |$unixid|\n";

> I checked and there are no terminating nor begining whitespace, here's
> a little more the code.
> # Traverse the UIDs_n_IDs_Not_Match file and Read & Substitute unix ids
> w/ NT ids.
> foreach (@name_change2) {
> my
> ($NULL1,$unixid,$NULL2,$unixuid,$NULL3,$NULL4,$nti d,$NULL5,$ntuid) =
> split /\s*\|\s*/;
> $line =~ s/$unixid/$ntid/g;
>
> } #end of foreach



Are you trying to match the string contained in $line (where is it
defined?) or $_ ? Do you have:
use strict;
use warnings;
in your code?


--
Glenn Jackman
NCF Sysadmin
(E-Mail Removed)
 
Reply With Quote
 
marcorice@gmail.com
Guest
Posts: n/a
 
      08-03-2005
>You seem to have ignored the advice I gave yesterday about how to clean
>this up.
> $line =~ s/$unixid/$ntid/g;
> } #end of foreach

I tried your suggestion from yesterday.
my ($unix_id2, $unixuid2, $ntid, $ntuid) = (split /\s*\|\*s/)[1,3,6,8];

it returns BLANK,and
$line =~ s/\Q^$unixid$\E/$ntid/g;
has the same affect as before and yes i'm using strict but not
warnings, it's not on my system.

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-03-2005
(E-Mail Removed) wrote:
> Paul Lalli wrote


(attribution added back in - please stop snipping that)

> >You seem to have ignored the advice I gave yesterday about how to clean
> >this up.
> > $line =~ s/$unixid/$ntid/g;
> > } #end of foreach

> I tried your suggestion from yesterday.
> my ($unix_id2, $unixuid2, $ntid, $ntuid) = (split /\s*\|\*s/)[1,3,6,8];
>
> it returns BLANK,and


I fail to believe you. Please post a short but complete script that
shows your version of that line succeeding, but mine failing.

> $line =~ s/\Q^$unixid$\E/$ntid/g;
> has the same affect as before and yes i'm using strict but not
> warnings, it's not on my system.


If your version of perl is old enough to not have warnings.pm, then are
you at least using
-w
on the shebang line?

One more time, I am asking you to post a short but COMPLETE program.
Until that happens, I'm done with this thread, and wish you well.

Paul Lalli

 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      08-04-2005
(E-Mail Removed) wrote:
> I'm trying to a substitute in a password and shadow file and perl will
> not let me do it for the end of string, can anyone help.
>
> #this works for begin of string.
> $line =~ s/^$unixid/$nitd/g;
> $line =~ s/\A$unixid/$nitd/g;
>
> #this does not for end of string.
> $line =~ s/$unixid$/$ntid/g;
> $line =~ s/$unixid\z/$ntid/g;
> $line =~ s/$unixid\Z/$ntid/g;
>
> #so therefore this does not for begin and end.
> $line =~ s/^$unixid$/$ntid/g;
>


Could it be that wour $line (still) contains a newline character at the
end, therefore having $unixid _not_ at the end?

perldoc -f chomp

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
marcorice@gmail.com
Guest
Posts: n/a
 
      08-04-2005
>One more time, I am asking you to post a short but COMPLETE program.
>Until that happens, I'm done with this thread, and wish you well.
>Paul Lalli

OKAY, here's the code, everthing works except when i want to do begin
and end substitution. I've only coding perl for a MONTH.
#!/bin/perl

use strict;
use Sys::Hostname;
use File::Copy;
chomp(my $date = `date '+%m%d%y'`);
#I know this looks a little messed up but works.
my $name_change = "./UIDs_n_IDs_Not_Match";
open(name_change,"$name_change") or die ("Can't open
$name_change:$!\n");
my @name_change2 = <name_change>;
# Checking Host
if ($host eq "powerhouse"){
# Setup output files
my $passwd_out = "passwd.out";
open (passwdout,">$passwd_out") or die ("Can't create $passwd_out
file: $!\n");
my $shadow_out = "shadow.out";
open (shadowout,">$shadow_out") or die ("Can't create $shadow_out
file: $!\n");
my $group_out = "group.out";
open (groupout,">$group_out") or die ("Can't create $group_out file:
$!\n");
my $passwd_file = "/etc/passwd";
my $shadow_file = "/etc/shadow";
my $group_file = "/etc/group";
copy("$passwd_file","$passwd_file.$date");
copy("$shadow_file","$shadow_file.$date");
copy("$group_file","$group_file.$date");
@ARGV=("$passwd_file","$shadow_file","$group_file" );
while (my $line=<>) {
#@pass_info = getpwnam((split(/:/,$_))[0]);

# Traverse the UIDs_n_IDs_Not_Match file and R&R unix ids w/ NT
ids.
foreach (@name_change2) {
my
($NULL1,$unixid,$NULL2,$unixuid,$NULL3,$NULL4,$nti d,$NULL5,$ntuid) =
split /\s*\|\s*/;
#THIS WORKS BUT I DON'T WANT TO USE "^$unixid:" I WANT TO USE
"^$unixid$"
$line =~ s/^$unixid:/$ntid:/g;

} #end of foreach

if ($ARGV eq "$passwd_file") { print passwdout $line; }
elsif ($ARGV eq "$shadow_file") { print shadowout $line; }
elsif ($ARGV eq "$group_file") { print groupout $line; }
}

# Close input files
close (name_change);
# Close output files
close (passwdout) or die ("Can't close passwd.out file; $!\n");
close (shadowout) or die ("Can't close shadow.out file; $!\n");
close (groupout) or die ("Can't close group.out file; $!\n");
move("$passwd_out","/tmp/passwd.mv") or warn "Move Failed: $!";
chmod(0444, "/tmp/passwd.mv") or warn "chmod Failed: $!";
} #end if

Here's an example of the UIDs_n_IDs_Not_Match file

UnixUserID | dopey | UID | 10 | does not match LOGIN and UID found in
LDAP | ldapusername | jdoe | LDAPUID | 106520 | UNIXNAME | John Doe |
LDAPname | John Doe | UnixEmployid | 00000 | LDAPemployid | 00000 |
email | (E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use python regular expression to substitute string value Allerdyce.John@gmail.com Python 7 02-26-2006 09:23 PM
How to remove and substitute characters within a string francescomoi@europe.com C Programming 4 04-30-2005 01:44 AM
Macro without string to substitute Casanova C Programming 6 11-24-2004 02:15 PM
Read an XQuery file, substitute a filename for a symbol and save in a string Jeff Kish Java 2 11-15-2004 11:10 AM
can string be a substitute of char s[] or char* s ? Lingyun Yang C++ 2 04-10-2004 02:41 PM



Advertisments