ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
My code is to delete last character (if last char is M or K) from a file. But it's printing out both the chop and un-chop result
Here's the output
new file: 10M
new file: 10
new file: 100M
new file: 100
new file: 100K
new file: 100
new file: 200K
new file: 200
Here's the code
my $file = "weekly.log";
# string to chop from log
my @strip = qw(M K);
open (FILE, "$file") or die "can't open $file: $!";
print "new file: $_\n";
You have two elements in the array @strip, and you iterate over then and on each iteration print the line... Move the print to outside of the foreach block. Also consider using a regular expression substitution:
awk is nice too, but I find it is slower for some operations. I once wrote some nice pretty reports using awk, which summarized many gigabytes of data. They were very nice - awk made implementing them a pleasure. Out of curiosity, one day, I fed my awk script through a2p - the automatic code converter from awk to perl. I ran the new perl script on the same input data, and it ran in less than half the time. Since then I haven't really used awk, apart from command-line one-liners.
Apart from my (very non-scientific) assessment of perl's superior performance, my biggest gripe with awk is this business of having whitespace as an operator in the language (string concatenation). I don't like this, although it is just a matter of personal taste.