Pulling data from a standard comment block - perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pulling data from a standard comment block - perl
# 1  
Old 03-24-2009
Pulling data from a standard comment block - perl

OK so I've inherited a set of scripts that do some work on a database. They do all have a standard comment block at the beginning that has good information on the script. I would like to generate a quick web page report that lists the script name and the description lines (for now it may be expanded in the future).

For reasons that aren't worth getting into the script that is making the page has to be a perl script.

The comment block format is as follows.

Code:
# Begin-Doc
##################################################################
# Name: <script name>
# Type: Script  (recursive or script it was called by) or web page or API
#
# Description:
# <enter a description of the script here (a concise 1 line description is fine). Also 
#       include other related scripts here.>  
#
# Tables: < Tables used in the script owner.tablename >
#
# Modes: <modes or other variable that plays a major role how the script 
#                       processes and a short description of each mode
#
# Functions:  <functions and a short 1 line description of that function> 
##################################################################
# End-Doc

Now I don't have problems writing the HTML from perl, and quick my @list = `ls $search_dir`; gets me the names.

And not all parts of that block are in every file. Some don't have any Table or Mode or Function sections so I don't always have a standard place to end my parsing.

I've managed to use sed (and I admit I'm really not good with sed or many other standard *nix tools) to pull just the line with Description but if it's multi line or the description starts on the next line I'm not getting things to work right.

The snipet that is working for the single line (and yes this parses subdirectories and creates links for the scripts as I will want that for some but for now am just doing it for everything) is as follows

Code:
my @list = `ls $search_dir`;

foreach my $temp (@list) {
    chomp $temp;
    unless ($temp =~ /.+\..*/) {
        print "<a class=directory href=\"#$temp\" onclick=\"toggle('$temp');\" class=title>$temp</a><br>\n",
              "<div id=\"$temp\" style=\"display:\">\n";
        my @curdirectory = `ls $search_dir/$temp`;
        foreach my $curapplication (@curdirectory) {
            chomp $curapplication;
            print "&nbsp;&nbsp;&nbsp;<a class=application href=\"https://$server/$sdir/$temp/$curapplication\" target=\"_blank\">$curapplication</a>";
            if ( my $desc_line = `sed -n '/# Description/{p;q;}' $search_dir/$temp/$curapplication` ) {
                $desc_line =~ s/^(# Description:)//i;
                print " - $desc_line<br>\n";
            }
            else { print " - No description found in header<br>\n"; }  
        }
        print "</div>\n";
    }
}

Which of course would work if the description were always a single line preceded by the # Description comment tag.

I've looked at the code at The sed (Stream Editor) FAQ - 4.21. How do I delete or change a block of text if the block contains a certain regular expression? (and other pages there0 that give examples on how to do several similar things, but my weakness with sed, grep, and regex is shining right now. I'm learning but I'd rather have this little web listing working sooner rather than later.

Anyone got some pointers for me? Yeah I'm a noob, and I have poked at some stuff on these forums but I'm still a bit stuck.
# 2  
Old 03-24-2009
Since you are using Perl and I don't know much sed, here's some perl to extract the description.
I guess your biggest problem will be identifying where the description ends. In the following I have put that task into a subroutine so that it can be easily modified and
for starters I assume that a line with only a # on it ends the description.
Code:
foreach my $curapplication ( @curdirectory ){

  unless( open( SRC, "<$curapplication") ) {
    print "Failed to open $curapplication: $!\n";
    next;
  }

  sub description_start {
    $_[0] =~ /^\#\s*Description:/;
  }  

  sub description_end {
    $_[0] =~ /^\#\s*$/;
  }

  my $description = 0;
  while( <SRC> ){
    description_start( $_ ) and $description = 1 and next;
    if( $description ) {
      description_end( $_ ) and $description = 0 and last;
      s/^\#//;
      print;
    }
  }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pulling information from a data file by date

awk -v now="$(date +%s)" -v tDiff="${USERMINUTES}" ' BEGIN { FS="=" if (!now) now=systime() if (!tDiff) tDiff=60*60 p=1 } /{/ {rec=$0;p=1;next} /}/ && rec && p {print rec ORS $0;next} $1=="entry_time" { if (now-$2>tDiff)p=0 } {rec=rec ORS $0}'... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. Shell Programming and Scripting

Pulling Data, Then Moving to the Next File

I'm scanning a list of emails- I need to pull 2 pieces of data, then move to the next file: Sender's Email Address Email Date I need these to be outputted into a single column- separated by a ",". Like this: Email1's Address, Email1's Date Stamp Email2's Address, Email2's Date Stamp... (4 Replies)
Discussion started by: sudo
4 Replies

3. Shell Programming and Scripting

BASH- Need help pulling data from .emlx

Hello, fellow computer junkies. First time poster! My boss wrote an application (Mavericks 10.9, Mountain Lion 10.8) that checks a user's security settings. The user runs the application, then it spits out an email that is sent back to our inbox showing the results. On our end, we have a mail rule... (5 Replies)
Discussion started by: sudo
5 Replies

4. Shell Programming and Scripting

Pulling data from xml

Hi there, Please could anyone help with this. I have an xml file that contains repeating values eg <Rule name> AAAAA <Action> BBBBB </Action> <Data> CCCCC </Data> <Type> DDDDD </Type> </Rule name> <Rule name> A1A1A1A1 <Action> B1B1B1B1 </Action> <Data> C1C1C1C </Data> <Type>... (4 Replies)
Discussion started by: ssideel
4 Replies

5. Shell Programming and Scripting

Pulling data by GPS coordinates from text file

Hi there, I'm having a problem trying to extract data from within a text file. I'm trying to extract this manually for a lack of better words. I need any items that fall within latitude 36.5 to 39.5 and long -75.3 to -83.9 I have been doing this using cat neta.txt | grep '!38' and working... (6 Replies)
Discussion started by: Mikey
6 Replies

6. Shell Programming and Scripting

Help with pulling / filtering data from a .csv

Good day Gurus, I have a csv file that contains an inventory of active servers. This csv file contains a well over a hundred systems (IBM, SUN, HP). It also contains those systems details. See below for an example hostA,invver,1.02,20100430 hostA,date,08/30/2010,06:18 hostA,use,"Unknown... (4 Replies)
Discussion started by: LuffyDMonkey
4 Replies

7. Shell Programming and Scripting

SFTP to server, pulling data and removing the data

Hi all, I have the following script, but are not too sure about the syntax to complete the script. In essence, the script must connect to a SFTP server at a client site with username and password located in a file on my server. Then change to the appropriate directory. Pull the data to the... (1 Reply)
Discussion started by: codenjanod
1 Replies

8. Shell Programming and Scripting

Block Comment in Shell script

how to put multiline comments in a shell script like /* Some code */ in C language? (3 Replies)
Discussion started by: skyineyes
3 Replies

9. Shell Programming and Scripting

Pulling data and following lines from file

I saw a few posts close to what i want to do, but they didn't look like they would work exactly.. or I need to think out of the box on this. I have a file that I keep server stats in for my own performance analysis. this file has the output from many commands in it (uptime, vmstats, ps, swap... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question