LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 10-29-2012, 01:21 PM   #1
Ziddy
LQ Newbie
 
Registered: Jun 2012
Location: Ontario
Distribution: BackTrack
Posts: 15

Rep: Reputation: Disabled
Question [Perl] Reading multiple lines from a file


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.

Thanks for your time.

Last edited by Ziddy; 10-29-2012 at 04:30 PM.
 
Old 10-29-2012, 01:48 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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.
 
Old 10-29-2012, 02:11 PM   #3
Ziddy
LQ Newbie
 
Registered: Jun 2012
Location: Ontario
Distribution: BackTrack
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.

Last edited by Ziddy; 10-29-2012 at 04:29 PM.
 
Old 10-30-2012, 07:27 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Your Perl.fu is better than mine.
 
  


Reply

Tags
perl, simple, stdin



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
[SOLVED] Perl: how to replace blank lines in a file with given lines from another karamaz0v Programming 8 04-19-2012 06:48 AM
[SOLVED] How to delete multiple lines in a file using perl yjy4321 Programming 14 04-21-2011 10:15 AM
Reading lines from files Perl jack.barnes Linux - Newbie 3 04-15-2011 04:05 AM
Reading lines within a file (Perl) jack.barnes Programming 12 03-16-2011 11:09 AM
[SOLVED] [Perl] fail to sort a file with 300,000 lines by multiple columns Kunsheng Programming 10 11-13-2009 06:41 PM

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

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

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