Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
printf is the "formatted printing" function. You define a fixed printing pattern to use as the first argument, and the input you want printed in that format as the subsequent arguments. If you want to insert a space (or a newline, or anything else) in the output, you have to put it in the format string.
See here for a full description of printf on gawk:
#!/usr/bin/perl
open (FILE, "/tmp/awk_loop") or die ("Can not open the File $!");
while (<FILE>) {
@array = <FILE> ;
foreach $line (@array) {
@split_array=split(" ", $line);
@splice = @split_array[2,7,13..24] ;
print "@splice\n";
}
}
this works almost - the formatting is good, but it does not print out the first line --t i guess i will have to open up another thread.
Last edited by casperdaghost; 12-30-2011 at 04:07 PM.
#!/usr/bin/perl
open (FILE, "/tmp/awk_loop") or die ("Can not open the File $!");
while (<FILE>) {
@array = <FILE> ;
foreach $line (@array) {
@split_array=split(" ", $line);
@splice = @split_array[2,7,13..24] ;
print "@splice\n";
}
}
I'm confident some of the resident perl fans can trim that down to three lines of under 80 characters each. Maybe even less.
the code works fine when I more the whole file and backtick it into an array - however that eats up a whole load of memory.
I need to read it into a FILEHANDLE and process it line by line.
There is something going on with the while loop and loading the FILEHANDLE into the array.
Nope, haven't written anything in perl for years, sorry. I find bash and awk are enough for almost anything I might want to do. When my needs are more complex I reach for C or Java, but even that is pretty rare.
I'm a rank beginner in perl myself. I know just enough to understand what that code is doing basically, but not enough to really help you with the problem.
It looks like it really isn't doing all that much that's different from awk though. Only instead of printing the fields directly, it extracts the fields you want from each line into an array, then prints that.
awk could handle it that way as well, but you'd have to deal with the limitations it has in sorting the array elements. It can sometimes be a bit frustrating to get awk to print its arrays in the order you want, as it doesn't keep track of the initial input order for the indexes.
I think what awk really needs is a way to print ranges of fields (and array elements) without having to use loops all the time, like you can do in perl. I really wonder why, in all the years it's been around, nobody has ever bothered to extend it to include such features.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.