finding a spot in a file

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Re: finding a spot in a file

    Chad Kellerman wrote:
    >
    > Hello,
    Hello,
    > I am opening up a file (actually a my.conf). I only want 2 lines in
    > the file. port and datadir
    >
    > The problem I am running into is that there is a port = 3324 in both
    > the [client] section and the [mysqld] section.
    >
    > I want to open the file and go straight to the [mysqld] section and
    > grab the values for port and datadir.
    >
    > Can anyone point me in the right direction?
    You could use a module like
    [url]http://search.cpan.org/author/WADG/Config-IniFiles-2.38/IniFiles.pm[/url] or
    [url]http://search.cpan.org/author/SHERZODR/Config-Simple-4.55/Simple.pm[/url]

    Or something like this may work:

    $/ = '[';
    my ( $port, $datadir );
    while ( <FILE> ) {
    if ( /mysqld]/ ) {
    $port = $1 if /^port\s*=\s*(\d+)/m;
    $datadir = $1 if /^datadir\s*=\s*(.+)/m;
    }
    }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  2. Similar Questions and Discussions

    1. Finding a Spot color Location
      Is there an easy way to find and change objects in a document making use of a specific spot color I inherited a document that has two spot colors...
    2. DCS File Not Saving All Spot Channels
      Help! I have a client who wanted to colorize a black and white drawing in order to print in spot colors...Now he wants to print in process because...
    3. Saving a spot color file for use with InDesign 2/PDFs
      We are updating old PageMaker documents in which we had placed 2 color spot color DCS files from Photoshop. A while back I had asked in the InDesign...
    4. imported photoshop eps spot file?
      I am putting together a 2 colour document, When i import the eps file it appears in black and white. Is this normal? or am i doing someting wrong? ...
    5. Limit to how many spot colours in an AICS eps file?
      Hi there. For the 2nd time I've had to use AI9 to make an eps or ps file from an AI file with lots of spot colours. The first time I was trying to...
  3. #2

    Default Re: finding a spot in a file

    On Thu, 09 Oct 2003 21:12:38 -0400, chad kellerman wrote:
    > Can anyone point me in the right direction?
    Seems like you're trying to read a configuration file. There are modules
    to do that work for you. Check out CPAN.


    --
    Tore Aursand <tore@aursand.no>

    Tore Aursand Guest

  4. #3

    Default Re: finding a spot in a file

    Chad Kellerman wrote:
    >
    > I am opening up a file (actually a my.conf). I only want 2
    > lines in the file. port and datadir
    >
    > The problem I am running into is that there is a
    > port = 3324 in both the [client] section and the [mysqld]
    > section.
    >
    > I want to open the file and go straight to the [mysqld]
    > section and grab the values for port and datadir.
    >
    > Can anyone point me in the right direction?
    Hi Chad.

    Does something like this help? It relies on both values
    actually being present in the section.

    Cheers,

    Rob


    use strict;
    use warnings;

    open CONF, 'my.conf' or die $!;

    $_ = <CONF> until /\Q[mysqld]/;

    my $port;
    my $datadir;

    while (<CONF>) {

    chomp;

    $port = $_ if /\bport\s*=/;
    $datadir = $_ if /\bdatadir\s*=/;

    last if $port and $datadir;
    }


    Rob Dixon Guest

  5. #4

    Default Re: finding a spot in a file

    John,

    Thanks that was perfect..



    Chad


    On Fri, 2003-10-10 at 03:11, John W. Krahn wrote:
    > Chad Kellerman wrote:
    > >
    > > Hello,
    >
    > Hello,
    >
    > > I am opening up a file (actually a my.conf). I only want 2 lines in
    > > the file. port and datadir
    > >
    > > The problem I am running into is that there is a port = 3324 in both
    > > the [client] section and the [mysqld] section.
    > >
    > > I want to open the file and go straight to the [mysqld] section and
    > > grab the values for port and datadir.
    > >
    > > Can anyone point me in the right direction?
    >
    > You could use a module like
    > [url]http://search.cpan.org/author/WADG/Config-IniFiles-2.38/IniFiles.pm[/url] or
    > [url]http://search.cpan.org/author/SHERZODR/Config-Simple-4.55/Simple.pm[/url]
    >
    > Or something like this may work:
    >
    > $/ = '[';
    > my ( $port, $datadir );
    > while ( <FILE> ) {
    > if ( /mysqld]/ ) {
    > $port = $1 if /^port\s*=\s*(\d+)/m;
    > $datadir = $1 if /^datadir\s*=\s*(.+)/m;
    > }
    > }
    >
    >
    >
    > John
    > --
    > use Perl;
    > program
    > fulfillment
    --
    Chad Kellerman
    Systems Administration / Network Operations Manager
    Alabanza Inc.
    10 E Baltimore Street
    Baltimore, Md 21202
    Phone: 410-234-3305
    Email: [email]ckellerman@alabanza.com[/email]


    Chad Kellerman Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139