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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello,
I had posted a short time ago about a problem I was having trying to sort through a file via awk over in the Solaris forum but while the solution worked I am trying to see if I can handle the same issue as a Perl program. What I am trying to do is to get a repeatable program that can target the 10th and 11th column of data and extract it from one file and place it into a new file. There will not always be an 11th column. I would like to get the results from each line of the input file concatonated onto the end of the previous line ignoring any newlines. The ability to designate files for input and output is relevant because I want to be able to reuse the same program several times in a row on several different files.
Code:
#!/usr/bin/perl -w
print "Input File Name: ";
$input = <>;
print "Output File Name: ";
$parse = <>;
open ($input, "$input");
open ($parse, "$parse");
while (<$input>) {
split (/ /);
print $parse "$column[10],column[11]\n";
}
close ($input);
close ($parse);
What I have been getting when I try to run this is an error for each line of the input file that reads as follows:
Code:
use of uninitialized value in concatenation (.) or string at ./script.pl line 11, <input_file.txt> line 1.
use of uninitialized value in concatenation (.) or string at ./script.pl line 11, <input_file.txt> line 1.
Filehandle output.pl opened only for input at ./script.pl line 11, <input_file.txt> line 1.
I am really trying to figure this out in Perl terms and I am learning on the fly. Thanks for your assistance in advance.
#!/usr/bin/perl
use strict ;
use warnings ;
print "Input File Name: ";
my $input = <>;
print "Output File Name: ";
my $parse = <>;
open (INPUT, $input);
open (PARSE, ">$parse"); # don't forget to open for writing
my @column ; # don't forget to initialize @column
while (<INPUT>) {
@column = split (/ /);
print PARSE "$column[1],$column[2]\n"; # I didn't have a file with that much columns ;)
}
close (INPUT);
close (PARSE);
Markus
Last edited by markush; 05-04-2012 at 05:18 PM.
Reason: added a comment
#!/usr/bin/perl
use strict ;
use warnings ;
print "Input File Name: ";
my $input = <>;
print "Output File Name: ";
my $parse = <>;
open (INPUT, $input);
open (PARSE, ">$parse"); # don't forget to open for writing
my @column ; # don't forget to initialize @column
while (<INPUT>) {
@column = split (/ /);
print PARSE "$column[1],$column[2]\n"; # I didn't have a file with that much columns ;)
}
close (INPUT);
close (PARSE);
Markus
And "INPUT" and "PARSE" are global - for now good reason.
You can write
Code:
open(my $ifh, $input) or die "cannot open '$input' file for reading: $!";
Also I noted one thing...I received a great number of errors that read:
Code:
Use of unititialized value in concatenation (.) or string at ./Script line 17, <INPUT> line #####.
Mind you when I look at the output it is all there. What I think is happening is that there are only certain lines where I have a Column 12...but i needed to capture them nevertheless. I am wondering if there is a way to tell the program to print any colums at or past a designated number so that it can find them automatically without a screen full of what appear to be errors. Thanks!
Also I noted one thing...I received a great number of errors that read:
Code:
Use of unititialized value in concatenation (.) or string at ./Script line 17, <INPUT> line #####.
Mind you when I look at the output it is all there. What I think is happening is that there are only certain lines where I have a Column 12...but i needed to capture them nevertheless. I am wondering if there is a way to tell the program to print any colums at or past a designated number so that it can find them automatically without a screen full of what appear to be errors. Thanks!
You have those warnings because your code tries to access array elements which do bot exist.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.