[SOLVED] [Perl] Reading multiple lines from a file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Hey there.
I'm not too sure how to phrase this, which seems to be why I'm having such a hard time finding an answer.
What I'm trying to do is...
Open a file and read the line. Say the line looks like this. "IP_Address username password". I want the IP in one variable, the UN in another and the PW in another. Then I do what I wish with them through other code, once finished, it moves on to the next line, formatted the same way. (IP UN PW).
Sorry that sounds so confusing. I know how read the entire line, then execute code and continue to the next, just not with arguments AFTER, on the same line.
Without knowing how much perl skill you have, here's a basic workup on opening a file and reading line-by-line while looking for text.
This code probably does NOT work OOTB. so some Perl kung-fu will be necessary.
Code:
my $dataFile = "/path/to/my/file";
open LOG, "<$dataFile";
while (<LOG>) {
$line = $_;
next if ($line=~ /^\s*#/); # ignore comment lines
if($line =~ /(\d+\.\d+\.\d+\.\d+)(\w+)(\w+)/)
print "$1 $2 $3\n";
I wrote a similar functioning script here... that probably can be modified to suit your purpose.
Without some sample data it would be a guessing game at best.
I evidently need to learn more about built in commands.
it spits out the ip for $1, but a number for $2/$3. Single digit.
Let's say in the file we have
11.22.33.44 hello world
22.33.44.55 world hello
$1 = 11.22.33.44
$2 = hello
$3 = world
is what I'm after. Then it executes code after assigning variables, moves on to the next line in the file, overwriting the variables, repeats until end of file. $1 becomes 22.33.44.55 after it's done with the 11.22.33.44 line. I know I'm making this more complicated than it has to be, as I usually do. Thanks fr your reply, I'm still messing with it.
Edit: I believe I'm on to something here. The only problem is not all lines have 3 arguments which seems to mess with it.
Edit2: Figured it out. Works quite well. Thanks for all the help. For those wondering:
open(INPUT_FILE, "check.txt") or die "check.txt NOT FOUND!\n";
my @list = <INPUT_FILE>;
close(INPUT_FILE);
for my $line (@list) {
if($line =~ /(\d+\.\d+\.\d+\.\d+) (\w+) (\w+)/) {
print "Checking $1 - $2 - $3:\t ";
}
etc etc.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.