LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 02-18-2007, 05:49 AM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
[Perl] chop last char from line


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

Quote:

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

Code:
#!/usr/local/bin/perl

my $file = "weekly.log";

# string to chop from log
my @strip = qw(M K);

open (FILE, "$file") or die "can't open $file: $!";

while (<FILE>)
{
    foreach $strip(@strip)
    {
        chop;
        print "new file: $_\n";
    }
}
 
Old 02-18-2007, 09:00 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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:
Code:
while(<FILE>) 
{
    s/[MK]$//;
    print;
}
 
Old 02-18-2007, 09:11 PM   #3
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by noir911
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

Here's the code

Code:
#!/usr/local/bin/perl
I would use awk rather than perl:

awk '{ sub(/[MK]$/,""); print }' "$FILENAME"
 
Old 02-19-2007, 01:38 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by cfaj
I would use awk rather than perl:

awk '{ sub(/[MK]$/,""); print }' "$FILENAME"
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.
 
Old 02-19-2007, 04:47 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
true lazy perl...
Code:
#!/usr/local/bin/perl -p

BEGIN {@ARGV = "weekly.log"}
s/[MK]$//;
 
Old 02-19-2007, 05:10 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
even lazier perl:
Code:
$ perl -npe 's/[MN]$//;' input_file > output_file
 
Old 02-19-2007, 06:47 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
even lazier perl
Code:
$ perl -pe 's/[MN]$//;' input_file > output_file

superfluous -n switch
 
Old 02-19-2007, 07:29 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I bow to your expert laziness!
 
Old 02-20-2007, 10:16 PM   #9
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Code:
sed 's/[MK]//g' b
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Perl char frequency in a string doesn't work richikiki Programming 9 07-21-2006 04:50 PM
perl regular expression a char match richikiki Programming 8 07-19-2006 03:37 AM
chop chop, dlink dwl 650 rev M problems? victory! rhoyerboat Linux - Wireless Networking 0 02-08-2005 06:04 AM
Read a char from a file (PERL) linuxlover1 Programming 4 01-09-2005 09:10 AM
perl: substring extraction after specific char markus1982 Programming 2 10-06-2002 05:48 AM


All times are GMT -5. The time now is 10:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration