SOS! how to do conditional search & replacement of strings in perl?

W

walala

Suppose I want to do conditional search & replacement:

I have the following input text file:

-------------------------------------------------------------------
_INST1832 B_1 59 135.301588688164E-18 M=1.0

_INST1833 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6
+AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6
+PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0
-------------------------------------------------------------------

I want to change the "_INST" in the lines where there is no "PCH" or "NCH"
patterns to "C", and change the "_INST" in the lines where there is "PCH" or
"NCH" to "M".

The result should be:

-------------------------------------------------------------------
C1832 B_1 59 135.301588688164E-18 M=1.0

M1833 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6
+AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6
+PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0
-------------------------------------------------------------------

What do I do? Can you give me the right "regular expression" which can do
such thing?

Thank you very much!

-Walala
 
G

Gunnar Hjalmarsson

walala said:
I want to change the "_INST" in the lines where there is no "PCH"
or "NCH" patterns to "C", and change the "_INST" in the lines where
there is "PCH" or "NCH" to "M".

What do I do?

You study Perl documentation:

perldoc perlre
perldoc perlop

Also available on the web: http://www.perldoc.com/

Then you write the code yourself. :)
Can you give me the right "regular expression" which can do such
thing?

Yes, there are quite a few people who can do that.
 
J

John W. Krahn

walala said:
Suppose I want to do conditional search & replacement:

I have the following input text file:
-------------------------------------------------------------------
_INST1832 B_1 59 135.301588688164E-18 M=1.0

_INST1833 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6
+AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6
+PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0
-------------------------------------------------------------------

I want to change the "_INST" in the lines where there is no "PCH" or "NCH"
patterns to "C", and change the "_INST" in the lines where there is "PCH" or
"NCH" to "M".

The result should be:
-------------------------------------------------------------------
C1832 B_1 59 135.301588688164E-18 M=1.0

M1833 S_4 47 52 VDD! PCH L=239.99999143598E-9 W=3.99999998990097E-6
+AD=2.04320002757108E-12 AS=1.58200004745507E-12 PD=5.36000015927129E-6
+PS=829.999976303952E-9 NRD=+2.50000001E-01 NRS=+2.50000001E-01 M=1.0


perl -pe'/PCH|NCH/ ? s/^_INST/M/ : s/^_INST/C/' yourfile



John
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
perl -pe'/PCH|NCH/ ? s/^_INST/M/ : s/^_INST/C/' yourfile

Or with a hint of evil obfuscation ;)

perl -pe 's#^_INST# /[PN]CH/ ? "M" : "C" #e' yourfile


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/bweieS99pGMif6wRAs5QAJ9loH3W2db0iE2AQ3+5+Br/Ghvh/wCfcp5P
aZcZXvivxYcCGA6dVYrBfb8=
=zV3t
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top