LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2008, 09:53 AM   #1
KClaisse
LQ Newbie
 
Registered: Jul 2007
Posts: 7

Rep: Reputation: 0
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.
 
Old 05-13-2008, 10:34 AM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 05-13-2008, 11:04 AM   #3
KClaisse
LQ Newbie
 
Registered: Jul 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by forrestt View Post
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.
 
Old 05-13-2008, 11:13 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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).

Last edited by matthewg42; 05-13-2008 at 11:15 AM. Reason: typo
 
Old 05-13-2008, 01:36 PM   #5
KClaisse
LQ Newbie
 
Registered: Jul 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
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.
 
Old 05-13-2008, 05:41 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,352

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl regex newbie lrt Programming 7 11-15-2007 08:27 PM
Perl Question for perl hackers. linuxlover1 Programming 1 06-27-2006 06:56 PM
reinstall perl question.. (urpme perl) rjcrews Mandriva 2 01-28-2006 06:19 PM
Perl array (newbie) question Seventh Programming 5 09-02-2004 12:11 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration