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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substitute chars with some variable value 1

Status
Not open for further replies.

ns705

Programmer
Feb 4, 2004
29
GB
Hi,
I'm very sorry for bothering you with such simple question.
The single excuse is I see Perl program at the first time in my life.
Can you please help me?
I have some file with records looking like:
/directory/subdir/unknowndir/file
I need to change this "unknowndir" with a value of some variable.
That is, with $variable="/newestdir", for example.
I need redo the above line into
/directory/subdir/newestdir/file.

Thank you in advance.
And sorry for bothering once more.

Anta
 
Code:
$path = "/directory/subdir/unknowndir/file";
$variable="/newestdir";
$path =~ s|/unknowndir|$variable|;

print "$path";


Kind Regards
Duncan
 
Duncan,

thank you a lot!
My problem was also I received that variable as
var=$ENV{USER}
And it didn't work while I changed var="$ENV{USER}"
To tell the truth, I don't quite understand what I have done :)
Oh, Perl is a real Terra Incognita for me.

Thank you.

Anta
 
that should not matter - for example my $ENV{USER} is currently set to 'Duncan' so:-

Code:
$path = "/directory/subdir/unknowndir/file";
$path =~ s|/unknowndir|/$ENV{USER}|;
print "$path";

returns:-

/directory/subdir/Duncan/file


Kind Regards
Duncan
 
Duncan,

thank you.
It helped me.

Anta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top