Perl regex to remove a segment in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl regex to remove a segment in a line
# 8  
Old 08-20-2012
It starts off very much like your original attempt.

\|~DRG\|(\d+): Begins by looking for |~DRG|<some number>. The number is captured.

.*?: ... followed by a non-greedy wildcard, so it doesn't skip anything we don't want to skip ...

(?=\|~(?!DCT)) ... until a |~ is found that is not followed by DCT. This uses a negative look-ahead assertion nested within a positive look-ahead assertion, so that we don't consume this portion of the record, leaving it for the substitution's next iteration.

/$1>15 ? "" : $&/ge: The e flag indicates that the replacement text should be treated as a perl expression. We test if the number captured by the first parenthetical, $1, is greater than 15. If it is, everything that was matched is replaced with an empty string. If $1 is not greater than 15, $& replaces the text with itself.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 9  
Old 08-20-2012
Actually I think I have it figured out. Please correct me if not.

-l = chomp each record (strips newlines)
-p = Automatically loop through each line and print it
-e = define code to be executed

s/\|~DRG\| = Search for the patterm "|~DRG|"
(\d+) = followed by a positive number (group it so we can refer to it as $1)
.*? = followed by any character, any number of characters stopping at the first match on the line of the second group:
(?=\|~(?!DCT)) = match where the pattern equals "|~" followed by a string that is NOT "DCT"
/$1>15?"":$& = replace that with the results of a test. If the first group (the positive nbr) is > 15, replace with an empty string. Else replace with the results of the last match (the segment itself).
/g = perform the replacements on every occurrence on the line
e = evaluate the replacement expression as if it is a Perl expression, rather than just a regular expression

Whew

---------- Post updated at 06:03 PM ---------- Previous update was at 06:02 PM ----------

You beat me to it! At least I was right in figuring it out.

Thanks a million!

Last edited by gary_w; 08-20-2012 at 07:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Need to remove first 6 lines and last line in a array ---- perl scripting

Hi I have stored a command output in an array like below @a = `xyz`; actually xyz comnad will give the output like this tracker date xxxxxxx xxxxxxx --------------------- 1 a 2 b ---------------------- i have stored the "xyz" output to an... (3 Replies)
Discussion started by: siva kumar
3 Replies

3. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

4. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

5. Shell Programming and Scripting

Using Sed to remove part of line with regex

Greetings everyone. Right now I am working on a script to be used during automated deployment of servers. What I have to do is remove localhost.localdomain and localhost6.localdomain6 from the /etc/hosts file. Simple, right? Except most of the examples I've found using sed want to delete the entire... (4 Replies)
Discussion started by: msarro
4 Replies

6. Shell Programming and Scripting

Remove repeated line using Perl

I am new to Perl and in text file of around 1000 lines having around 500 repeated line which I felt is no use and want to remove these line.so can somebody help in same for providing sample code how can i remove these repeated line in a file. (11 Replies)
Discussion started by: dinesh.4126
11 Replies

7. Shell Programming and Scripting

perl regex multi line cut

hello mighty all there's a file with lots of comments.. some of them looks like: =comment blabla blablabla bla =cut i'm trying to cut this out completely with this code: $line=~s/^=.+?=cut//sg; but no luck also tryed to change it abit but still I don't understand how the... (9 Replies)
Discussion started by: tip78
9 Replies

8. Shell Programming and Scripting

how to remove line from /etc/vfstab using shell / perl

Hi, could someone help me on this i want to remove line from /etc/vfstab in the system how to do that it is rite now like this /dev/vx/dsk/appdg1/mytestvol /dev/vx/rdsk/appdg1/mytestvol /mytest vxfs 3 no largefiles /dev/vx/dsk/appdg1/mytestvol1 ... (2 Replies)
Discussion started by: tarunn.dubeyy
2 Replies

9. Shell Programming and Scripting

Perl REGEX - How do extract a string in a line?

Hi Guys, In the following line: cn=portal.090710.191533.428571000,cn=groups,dc=mp,dc=rj,dc=gov,dc=br I need to extract this string: portal.090710.191533.428571000 As you can see this string always will be bettween "cn=" and "," strings. Someone know one regular expression to... (4 Replies)
Discussion started by: maverick-ski
4 Replies

10. Shell Programming and Scripting

how do i strip this line using perl regex.

I have a variable dynamically generated $batch = /dataload/R3P/interface/Bowne/reports/RDI00244.rpt Now I'd like to strip '/dataload/R3P/interface/Bowne/reports/RDI' and '.rpt' from this variable my output should be only 00244 how to do this using perl regex.I'm a newbie to perl and would... (1 Reply)
Discussion started by: ramky79
1 Replies
Login or Register to Ask a Question