Perl newbie - regex replace all groups issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl newbie - regex replace all groups issue
# 1  
Old 12-28-2011
regex replace all groups issue

Hello,

Although I have found similar questions, I could not find
advice that could help with my problem. The issue:

I am trying to replace all occurrences of a regex, but
I cannot make the regex groups work together.

This is a simple input test file:
Code:
The Vedanta Philosophy
~~~~~~~~~~~~~~~

Body text

This is what the output should be:
Code:
## The Vedanta Philosophy

Body text

This is my code:
Code:
#!/usr/bin/perl -w

use strict;

while (<>) {
    s/(^.*)\n(^~+)/## $1/igm;
    print;
}


sub usage {
    (my $progname = $0) =~ s/.*[\\\/]//;
    die "Usage: $progname [<file>...]\n";
}

The two groups
Code:
(^.*)

and
Code:
(^~+)

match the test file independently, but when
I put them together in the s///igm line, they do not work at all.

I read the perl regex introduction and tutorial, and
searched for hints where I am making a mistake, with no avail.

Smilie

Thank you for any help or indication on how to solve this problem.

Last edited by samask; 12-28-2011 at 02:03 PM.. Reason: minor correction
# 2  
Old 12-28-2011
Hi samask,

Some issues:

1.- while (<>) reads one line at a time and your regex tries to work over multiple lines.
2.- '^' marks the beginning of the line and it is zero-width, put it out of the parentheses.
3.- There is no need of the 'g' switch. Your regex only will match at most once.
4.- No need of 'i' switch. You are not using any alphabetic character.

One solution:

Code:
$ cat script.pl
use warnings;
use strict;

local $/;
my $file = <>;
$file =~ s/^(.*)\n^(~+)/## $1/m;
print $file;
$ cat infile
The Vedanta Philosophy
~~~~~~~~~~~~~~~

Body text
$ perl script.pl infile
## The Vedanta Philosophy

Body text

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 12-28-2011
you can do below if interested with one liner:-

Code:
perl -07 -wlpe 's/(.*)\s+~+/## $1/s ;' infile.txt

SmilieSmilieSmilie
This User Gave Thanks to ahmad.diab For This Post:
# 4  
Old 12-28-2011
Wow, thank you so much birei!

Thank you for the explanation, and also for your code.
It is just elegant, and it works perfectly.

Thank you so much once again.

Sam

---------- Post updated at 11:31 ---------- Previous update was at 11:29 ----------

Thank you ahmad.diab!

I like one liners, but, as I will add more regex logic, it may be more practical as separate script.

Thank you!

Sam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex - Capturing groups

I am having trouble with regex capturing groups, For Ex : I am having a file with ABC CDLF SFSDFK PRIMARY INDEX(XYZ,DEF,GHI); XYZ FLJ SDFKLD; PRIMARY INDEX(ABC); BHI SDKFLFLSFD PRIMARY INDEX (QWE , RTY , LHJ); My output should be : ABC XYZ,DEF,GHI XYZ ABC BHI ... (10 Replies)
Discussion started by: ysvsr1
10 Replies

2. Shell Programming and Scripting

Python Newbie Question Regex

I starting teaching myself python and am stuck on trying to understand why I am not getting the output that I want. Long story short, I am using PDB for debugging and here my function in which I am having my issue: import re ... ... ... def find_all_flvs(url): soup =... (1 Reply)
Discussion started by: metallica1973
1 Replies

3. Shell Programming and Scripting

Perl:Regex for Search and Replace that has a flexible match

Hi, I'm trying to match the front and back of a sequence. It works when there is an exact match (obviously), but I need the regex to be more flexible. When we get strings of nucleotides sometimes their prefixes and suffixes aren't exact matches. Sometimes there will be an extra letter and... (2 Replies)
Discussion started by: jdilts
2 Replies

4. Shell Programming and Scripting

perl regex string match issue..kindly help

i have a script in which i need to skip comments, and i am able to achieve it partially... IN text file: {**************************** {test : test...test } Script: while (<$fh>) { push ( @data, $_); } if ( $data =~ m/(^{\*+$)/ ){ } With the above match i am... (5 Replies)
Discussion started by: avskrm
5 Replies

5. Shell Programming and Scripting

perl regex issue

Hi, I find it really strange while writing a simple regex to match and print the matched string, dibyajyo@fwtest:~ #perl -e '$x = "root@rashmi>"; print "matched string:$1\n" if ($x =~ /(root@rashmi)/);' matched string:root dibyajyo@fwtest:~ #perl -e '$x = "root@rashmi>"; print... (1 Reply)
Discussion started by: rrd1986
1 Replies

6. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

7. Shell Programming and Scripting

Need help with regex groups

I have a requirement - replace specified positions in a string with a character. I found perl regex useful for this approach. however, I am facing the following issue. The target file 'temp' contains - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx The goal is to convert... (5 Replies)
Discussion started by: sam_roy
5 Replies

8. 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

9. Shell Programming and Scripting

Grep regex matches, groups

Hello, I am searching all over the place for this, just not finding anything solid :( I want to do be able to access the groups that are matched with grep (either with extended regex, or perl compatible regex). For instance: echo "abcd" | egrep "a(b(c(d)))" Of course this returns... (1 Reply)
Discussion started by: Rhije
1 Replies

10. UNIX for Dummies Questions & Answers

Newbie Regex Question

Hello, I am trying to use the CDE File Manager in AIX to filter out files that I want to be hidden in the file manager. It gives me a script box that I can supposedly enter a regex into if I want to filter out additional file types. Example: "Also Hide:"_______________ I can put... (0 Replies)
Discussion started by: ciremg01
0 Replies
Login or Register to Ask a Question