Link to home
Start Free TrialLog in
Avatar of mikeysmailbox
mikeysmailbox

asked on

Perl - Translate from ^ to #

Hi,

I need to scan through a file and change a line that has a # in it.
But I only need to change the line from ^ to the #

example from

     abcdesf#TOPS_TEST

example to

     ABCDEF#TOPS_TEST


Thanks
Avatar of FishMonger
FishMonger
Flag of United States of America image

Do you need to change A line or ALL lines that match that pattern?

This will do an "inline edit" and change all lines that match that pattern.
perl -ibak -pe 's/^([a-z]+?#)/\U$1/i' file.txt
Avatar of mikeysmailbox
mikeysmailbox

ASKER

FishMonger,

It did not work. I am changing ALL lines.

Thanks
Humm...it worked for me, but if you're on a Windows box, we'll need to use double quotes instead of the single quotes.

Here's my test.

[root@fc4dev ~]# cat mike
abcdesf#TOPS_TEST
abcdesfTOPS_TEST
wgfBVA#BOTTOMS_UP_TEST
wgfBVABOTTOMS_down_TEST
[root@fc4dev ~]#
[root@fc4dev ~]# perl -ibak -pe 's/^([a-z]+?#)/\U$1/i' mike
[root@fc4dev ~]#
[root@fc4dev ~]# cat mike
ABCDESF#TOPS_TEST
abcdesfTOPS_TEST
WGFBVA#BOTTOMS_UP_TEST
wgfBVABOTTOMS_down_TEST

Can you post a sample of your real data?  
I need to keep the #

example before

SCHEDULE METINT444#BEMAW_NEF
         ON      REQUEST, su
except 9/25/05

         AT      1700
         OPENS   AXRS6LPAR05#"/u/emap/ctldir/E8844PS.NEF.TRIGGER.Z*"
:
   metint444#BEMA_PREDELCACHE4
   metint445#BEMA_PREDELCACHE4_MIS02
END



Example After

SCHEDULE METINT444#BEMAW_NEF
         ON      REQUEST, su
except 9/25/05

         AT      1700
         OPENS   AXRS6LPAR05#"/u/emap/ctldir/E8844PS.NEF.TRIGGER.Z*"
:
   METINT444#BEMA_PREDELCACHE4
   METINT445#BEMA_PREDELCACHE4_MIS02

END


I need to change the metint444 and metint445 to uppercase


Thanks
The reason the regex failed is because the lines you want changed have leading spaces, but your first example didn't.

change the regex to this:
s/^(\s*[a-z]+?#)/\U$1/
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops, lets try again...it's been a long day and my eyes are blury. :)

s/^(\s*\w+?#)/\U$1/