Operating System - HP-UX
1821638 Members
3108 Online
109633 Solutions
New Discussion юеВ

perl substitution question

 
SOLVED
Go to solution
Jeff Picton
Regular Advisor

perl substitution question

Hi all

I have a file as follows :-

juve00_04070706

and require to substitute using perl syntax =~s/ etc everything after the _ to become :-

juve00_********

Can anyone help ?

6 REPLIES 6
Tom Maloy
Respected Contributor

Re: perl substitution question

You can try this:

echo test_change | perl -p -e 's/_.*/done/'

which will change test_change to testdone.

If you are just trying to change a file name while running ksh, you do not need to use perl, you can use this:

fn=test_change
newfn=${fn%_*}done
echo $newfn

The "%" tells ksh to remove the small right pattern that matches "_*", where "*" is a wild card that matches all characters.
Carpe diem!
Jeff Picton
Regular Advisor

Re: perl substitution question

Hi

This is embedded within a perl script, so I am looking for something like :-

filename=juve00_04050607

$filename2=~s/_*/********/ (but this does not work)

I need to substitute everything after the underscore to be *

Thanks
RAC_1
Honored Contributor

Re: perl substitution question

Not per, just awk.

echo "juve00_04050607"|awk -F "_" '{print $1,"_********"}'

Anil
There is no substitute to HARDWORK
Jeff Picton
Regular Advisor

Re: perl substitution question

Hi

It must be perl - some sort of substitution or translation, i think
Hein van den Heuvel
Honored Contributor
Solution

Re: perl substitution question


To indicate a series of anything you use .* in perl, not just the *
So in it's simplest form your problem can be solved by:

$filename=shift; # take from command line
$filename=~ s/_.*$/_******/;
print "$filename\n";


But what is your exact problem definition?
The last underscore?
Replace the tail wih a fixed series of stars, or wiht as many stars as there where characters? numbers only?
Here is a more elaborate solution replacing the chracters following the last understcore with as many stars:

$filename=shift;
if ($filename =~ /(.*_)(.*)$/) {
$head = $1;
$tail = $2;
$tail =~ s/./*/g;
$filename = $head . $tail;
}
print "$filename\n";


You may want to read up more on regular expression, the 'tr' function, the 's' functions and perhpas also check out 'rindex' and 'substr'.

Enjoy,
Hein.


Jeff Picton
Regular Advisor

Re: perl substitution question

Thanks Hein you got what I anted