LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A newbie perl question (https://www.linuxquestions.org/questions/programming-9/a-newbie-perl-question-641897/)

KClaisse 05-13-2008 09:53 AM

A newbie perl question
 
Im very new to perl, and I can't seem to figure this out:

I have a text file named data.txt that looks like this:
Code:

--BEGIN DATA--
HTTPD,1
FTP,1
SMTPD,1
OPENFIRE,1
TEST,0
--END DATA--

Im trying to figure out how I would read this file and then output to an html page in a format such as this:
Code:

HTTPD - on
FTP - on
SMTPD - on
OPENFIRE - on
TEST - off

I know how to open a file and lock it, but after that im lost.

forrestt 05-13-2008 10:34 AM

Try this:

Code:

#!/usr/bin/perl

$FILE = '/path/to/datafile';
open(FILE, "$FILE") || die "Error: Couldn't open file $FILE";
    while (<FILE>) {
        # Ignore comments and blank lines and the begin/end data lines
        if ((!/^\s*(#|\n)/) && (!/^--/)) {
            ($name, $value) = split(',');
            chomp($value);
            print "$name - ";
            if ($value == 1) {
                print "on\n"
            } else {
                print "off\n"
            }
        }
    }
close(FILE);

HTH

Forrest

KClaisse 05-13-2008 11:04 AM

Quote:

Originally Posted by forrestt (Post 3151910)
Try this:

Code:

#!/usr/bin/perl

$FILE = '/path/to/datafile';
open(FILE, "$FILE") || die "Error: Couldn't open file $FILE";
    while (<FILE>) {
        # Ignore comments and blank lines and the begin/end data lines
        if ((!/^\s*(#|\n)/) && (!/^--/)) {
            ($name, $value) = split(',');
            chomp($value);
            print "$name - ";
            if ($value == 1) {
                print "on\n"
            } else {
                print "off\n"
            }
        }
    }
close(FILE);

HTH

Forrest

Thank you so much, that works perfectly. It looks like I've got a lot to learn.

matthewg42 05-13-2008 11:13 AM

Although it doesn't contain any file locking stuff, you can do the whole lot as a one liner:
Code:

perl -i -n -e 'if (!/^--/) { s/,1/ - on/; s/,0/ - off/; print; }' data.txt
Some explanation:

The -i means the output of the program is saved into the input file.

The -n means to execute the program for each line of the input file(s).

The -e means that the following string (enclosed in single quotes) is the program. Here it is with some formatting to help you read it:
Code:

if (!/^--/) {
    s/,1/ - on/;
    s/,0/ - off/;
    print;
}

The clause of the if says, "only do this for line which don't begin with "--".
Inside the if block are two replacements, which substitute ",1" for " - on", and ",0" for " - off". The line is then printed.

Note that none of these operations say what they are working on. This is a feature of Perl that many operations have a default argument, which is usually the $_ variable. This means "the current input line" as set by the <FILEHANDLE> operator, or in the case of your using the -n option, each line of the input file(s).

KClaisse 05-13-2008 01:36 PM

Quote:

Originally Posted by matthewg42 (Post 3151947)
Although it doesn't contain any file locking stuff, you can do the whole lot as a one liner:
Code:

perl -i -n -e 'if (!/^--/) { s/,1/ - on/; s/,0/ - off/; print; }' data.txt
Some explanation:

The -i means the output of the program is saved into the input file.

The -n means to execute the program for each line of the input file(s).

The -e means that the following string (enclosed in single quotes) is the program. Here it is with some formatting to help you read it:
Code:

if (!/^--/) {
    s/,1/ - on/;
    s/,0/ - off/;
    print;
}

The clause of the if says, "only do this for line which don't begin with "--".
Inside the if block are two replacements, which substitute ",1" for " - on", and ",0" for " - off". The line is then printed.

Note that none of these operations say what they are working on. This is a feature of Perl that many operations have a default argument, which is usually the $_ variable. This means "the current input line" as set by the <FILEHANDLE> operator, or in the case of your using the -n option, each line of the input file(s).

Nice descriptions. Thanks.

chrism01 05-13-2008 05:41 PM

If you haven't got it already, I highly recommend bookmarking this: http://perldoc.perl.org/
See the top left of that page for tutorials


All times are GMT -5. The time now is 06:57 AM.