onicom has asked for the wisdom of the Perl Monks concerning the following question:

Sorry for yet another regex question, but I have exhausted all my knowledge of regex and need guidance please on solving a problem.

I have inherited some java code which was originally created using Poseidon modelling tool. We no longer use the modelling tool but the code has lots of lovely tags left behind which I was going to remove with a 'quick' perl script (but now I'm stuck).

Each file has a number of tags that look like the following
* @poseidon-object-id [ Im39098af8m115b2fe9698mm7dca ]
N.B. The above line starts with a space just for clarication and the part in the bracket is always different

I have tried a couple of routines which traverse the filesystem (which I found on this site)
#!/usr/bin/perl -w # Update: Script should now work without generating # any warnings concering the %skip hash. Thanks to # jmcnamara for helping with that :) # This script will remove carriage returns (\r) from # all files located below a specified parent directory. # Could be expanded to allow any modifications. use strict; use File::Find; use File::Basename; #################################### # Configuration # Directory where recursive modification will begin my $dir = "/home/anthony/Development/Utils/temp1"; # File extensions to skip (ie: images) # Each entry has the following look: # 'file extension' => 1 my %skip = ( 'gif' => 1, 'jpg' => 1, 'jpeg' => 1, 'png' => 1 ); #################################### # Main Program - No Need To Edit :) my $count; # Just a counting thing my @dirs; # All recursed directories # This does at least 85% of the work! # It gets a recursive list of all directories find(\&{ sub { if (-d "$File::Find::dir/$_") { if ($count++ == 0) { push @dirs, $File::Find::dir; } else { push @dirs, $File::Find::dir . "/$_"; } } } }, $dir); # Loop through all directories foreach my $curDir (@dirs) { # Get list of files from the directory opendir DIR, $curDir; my @files = readdir DIR; closedir DIR; # Loop through the files foreach my $file (@files) { # Skip directories and "." and ".." entries # as well as excluded file extensions my ($nil,$ext) = $file =~ /^(.*?)\.(.*?)$/gs; $ext = '' unless defined $ext; next if (-d "$curDir/$file" || $file =~ /^\./ || $skip{$ext}); # Read in the file open FILE, "$curDir/$file"; my @lines = <FILE>; close FILE; # Modify and print the file back open FILE, ">$curDir/$file"; print FILE map { s/.*\@[poseidon]$/ /g; $_; } @lines; close FILE; } }

I have also tried a couple of simpler CLI's such as
perl -p -i -e 'm/^@poseidon//g' AbstractEnum.java

But I am just either removing only the word 'poseidon' or I delete the entire contents of the file!

All I would like to do is to remove the entire line which starts with

* @poseidon ...

Could someone kindly help please.

Many thanks in advance.

20090501 Janitored by Corion: Changed PRE to code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Removing @poseidon tags from java code
by almut (Canon) on Apr 28, 2009 at 21:32 UTC

    All I would like to do is to remove the entire line which starts with

    * @poseidon ...

    print FILE map { s/.*\@poseidon$/ /g; $_; } @lines;

    Instead of the $, you want to match the remainder of the line there (otherwise it won't match/be substituted), i.e.

    print FILE map { s/.*\@poseidon.*/ /g; $_; } @lines; ^^
Re: Removing @poseidon tags from java code
by ikegami (Patriarch) on Apr 28, 2009 at 21:34 UTC

    [ You'll help us both by using <c>..</c> instead of <pre>..</pre> ]

    For the one-liners,

    perl -i.bk -pe's/^\@poseidon.*//s' AbstractEnum.java

    or

    perl -i.bk -ne'print if !/^\@poseidon/' AbstractEnum.java

    Sorry, all I have time for right now.

Re: Removing @poseidon tags from java code
by Praethen (Scribe) on Apr 28, 2009 at 21:38 UTC

    Without understanding the full issue, if I were you, I'd try the following:

     s/.*\@poseidon.*\]/ /g;

    As long as you are sure that it always starts with that same string. I'd throw a \n tag metacharacter after the end square bracket to take out the line break in the files.

    BTW, I believe the $ tag indicates that the text must be at the end of the line being checked, so your regex would only remove poseidon (and everything before it on the line)

Re: Removing @poseidon tags from java code
by onicom (Initiate) on Apr 29, 2009 at 10:05 UTC
    Many thanks all that contributed. I misunderstood the $ directive. Problem solved.