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
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
ASKER
FishMonger,
It did not work. I am changing ALL lines.
Thanks
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?
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?
ASKER
I need to keep the #
example before
SCHEDULE METINT444#BEMAW_NEF
ON REQUEST, su
except 9/25/05
AT 1700
OPENS AXRS6LPAR05#"/u/emap/ctldi r/E8844PS. NEF.TRIGGE R.Z*"
:
metint444#BEMA_PREDELCACHE 4
metint445#BEMA_PREDELCACHE 4_MIS02
END
Example After
SCHEDULE METINT444#BEMAW_NEF
ON REQUEST, su
except 9/25/05
AT 1700
OPENS AXRS6LPAR05#"/u/emap/ctldi r/E8844PS. NEF.TRIGGE R.Z*"
:
METINT444#BEMA_PREDELCACHE 4
METINT445#BEMA_PREDELCACHE 4_MIS02
END
I need to change the metint444 and metint445 to uppercase
Thanks
example before
SCHEDULE METINT444#BEMAW_NEF
ON REQUEST, su
except 9/25/05
AT 1700
OPENS AXRS6LPAR05#"/u/emap/ctldi
:
metint444#BEMA_PREDELCACHE
metint445#BEMA_PREDELCACHE
END
Example After
SCHEDULE METINT444#BEMAW_NEF
ON REQUEST, su
except 9/25/05
AT 1700
OPENS AXRS6LPAR05#"/u/emap/ctldi
:
METINT444#BEMA_PREDELCACHE
METINT445#BEMA_PREDELCACHE
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/
change the regex to this:
s/^(\s*[a-z]+?#)/\U$1/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops, lets try again...it's been a long day and my eyes are blury. :)
s/^(\s*\w+?#)/\U$1/
s/^(\s*\w+?#)/\U$1/
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