 |
 |
perl script in sh
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
I can embed an awk script in sh and it will run. I can’t make a perl script run. Any help?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status:
Offline
|
|
Originally Posted by imdipped
I can embed an awk script in sh and it will run. I can’t make a perl script run. Any help?
I don't know perl, so correct me if I'm wrong... isn't that what perl -e is for?
|
"In Nomine Patris, Et Fili, Et Spiritus Sancti"
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Apr 2002
Location: case.edu
Status:
Offline
|
|
perl -e should do it.
Can you give an example?
|
pb 1440x960 | 1.67, 1.5, 128, 80 | leopard
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Tesseract, this is a very crude program because I am not a programmer. There is still a lot that I want to do with it, but first I want to be able to have it run under the shell. When I run it in Perl, it works.
This is the Perl script I want to add after the #!/usr/bin/sh line.
# program to count the number of alpha letters and the frequency of each letter
use warnings;
use strict;
while(<DATA>) {
s/ //g;
my $text = $_;
chomp $text;
my $CharCount = 0;
$CharCount = length($text);
print "Length of cipher is $CharCount charactors","\n\n";
print "Individual character count: \n";
my %count = frequency($text);
my @count = values(%count);
foreach my $key (sort {$count{$b} <=> $count{$a}} keys %count) {
print "$key-$count{$key}, "
}
print "\n";
}
sub frequency {
my $text = join('', @_);
my %letters;
foreach (split //, $text) {
$letters{$_}++;
}
return %letters;
}
__DATA__
FDRJN UHVXX URDMD SKVSO PJRKZ DYFZJ XGSRR VTQYR WDARW DFVRK VDRKV TDFSZ ZDYFR DNNVO VTSXS AWVZR
########## The output should be: ##########
#Length of cipher is 80 charactors
#Individual character count:
#R-11, D-10, V-9, S-6, Z-5, F-5, X-4, K-4, N-3, T-3, J-3, W-3, Y-3, O-2, U-2, A-2, Q-1, G-1, H-1, M-1, P-1,
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2004
Status:
Offline
|
|
imdipped, dunno what you're wanting to do exactly but if your perl script is in its own file, say, macnn.pl then you just can just call it in your shell script. No need to do the `perl -e` unless it was a conveniently short script like this in-place search and replace:
perl -i.bak -pe 's/num_threads\">1000<\/property>/num_threads\">500<\/property>/' installstat-*.jmx
Anyway, don't want to confuse the issue at hand. If you already have the perl script in its own file, then just call it in your shell script:
#!/bin/sh
macnn.pl
That's it. Now if you're wanting to take the output and do something with it, then set it to a variable:
OUTPUT="`macnn.pl`"
echo $OUTPUT
This will echo out the regular output you're expecting. Well, sort of. The newlines won't be there.
Whatever. Hope that helps.
Uber
|
|
PB17" | 1.33 | 1g | 80g
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Uber,
After some trial and error, I was able to do it this way.
-----------------------------------------------
#! /usr/bin/sh
perl -F filename # filename being the name of my executable Perl program
-----------------------------------------------
I am still running Mac OS 9.2.2 on my PowerMac G4 400. The only way I can access the UNIX environment is by using MachTen 4.1.4. That is why my shebang line is #! /usr/bin/whatever.
I will now work on that
OUTPUT="`macnn.pl`"
echo $OUTPUT
thing.
Thank you and the other posters very much.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
If it's just Perl, all you need to do is change your #! line to:
#!/usr/bin/perl
And then run it like any other script.
Am I missing something?
|
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Wevah,
I’m seventy one years young and my hobby is solving undivided simple substitution ciphers. I wanted to simplify the dog work that is required before solving.
I originally tried to do what I wanted to do in Perl because I thought it was the best tool to use when it came to text manipulation. I have no programming experience and soon found myself in over my head.
I fell back to the Bourne shell because there are letter counting commands and a few other useful ones but I wound up having to massage the data because it counts the spaces and maybe newlines.
I then moved to awk because it is simpler than Perl, user friendly, and can do almost everything I want it to do. It even has a better report generation capability (I think). I thought I would use a little of each scriptor, and use the shell as a wrapper.
I would only use Perl for its tr function. It has the capability to count the number of characters cleanly if you don’t substitute. The shell tr utility doesn’t.
What started out as a simple idea quickly got complex. I wound up using Perl more and more and went as far as I could go. I couldn’t figure out how to combine the characters that had the same frequency count and group them alphabetically behind their frequency count; ( from this N-3, T-3, J-3, W-3, Y-3, to this 3-JNTWY ). So I decided to stop and continue with the shell and awk. When I tried to embed the script in the shell. I couldn’t grok it.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
That's rad. I think I might try to take a whack at doing what you mentioned in Perl.
|
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
Here's a stab (though It may be too late now, it was kind of fun...). The subroutine returns an array whose values are sorted strings containing the characters whose frequency is the associated key. Then you can print out or otherwise use the strings as in the bottom for loop.
This example is run like so: ./charfreq 'foo', where 'foo' is the string you want to find the letter frequency of. The output for a literal 'foo' will be as follows:
1-F
2-O
This can probably be cleaned up, but as far as I can tell, it works well.
Code:
#!/usr/bin/perl
use strict;
use warnings;
sub charFreq($) {
my $str = uc shift;
my %freqs = ();
my @reverseFreqs;
my @ret;
for (my $i = 0; $i < length $str; ++$i) {
++$freqs{substr $str, $i, 1};
}
for (keys %freqs) {
push @{$reverseFreqs[$freqs{$_}]}, $_;
}
for (my $i = 0; $i < scalar @reverseFreqs; ++$i) {
if ($reverseFreqs[$i]) {
$ret[$i] = join '', sort @{$reverseFreqs[$i]};
}
}
return @ret;
}
my @freqs = charFreq $ARGV[0];
for (my $i = 0; $i < scalar @freqs; ++$i) {
if ($freqs[$i]) {
print "$i-$freqs[$i]\n";
}
}
|
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
Oh, this will also count punctuation and not just alphanumerics, so if you get a line like
8-
It's just counting spaces (silly me).
Splitting it first as you did is probably better, too...I didn't re-read your current script before writing this up. 
|
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Apr 2002
Location: case.edu
Status:
Offline
|
|
You can save your perl script in a separate file and invoke it from the shell script. That would save the trouble of escaping special characters to embed your perl code in the shell script. You could do something like:
Code:
#!/bin/sh
# this is the file foo.sh
echo "FDRJN UHVXX ..." | perl bar.pl
Code:
#!/usr/bin/perl
# this is the file bar.pl
while(<STDIN>) {
# process
}
|
pb 1440x960 | 1.67, 1.5, 128, 80 | leopard
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2003
Status:
Offline
|
|
Wevah and Tesseract,
Thank you for hanging in there. I thought the thread had wound down.
I am going to take your advice Tesserat and not try to keep the entire program embedded in the shell. I originally wanted to have a self contained program.
I only use the DATA filehandle to keep the files to a minimum and to illustrate what I’m doing in case you wanted to run it. I actually use input and output filehandles.
Wevah, your code looks interesting. I will work with it and see how I might make use of it.
This is what I came up with over the weekend.
%%%%%%%%%%%%%%%%%%% The program is now %%%%%%%%%%%%%%%%%%%
#!/usr/bin/perl -w
use strict;
my $text = $_;
while(<DATA>) {
s/ //g;
$text = $_;
chomp $text;
}
my $LetterCount = length($text);
my %FreqCount = frequency($text);
#print "$LetterCount letters","\n\n";
#print "Individual letter count: \n";
foreach (sort {$FreqCount{$b} <=> $FreqCount{$a}} keys %FreqCount) {
print $FreqCount{$_}, ":", "$_ ";
}
sub frequency {
my $text = join('', @_);
my %letters;
foreach (split //, $text) {
$letters{$_}++;
}
return %letters;
}
__DATA__
FDRJN UHVXX URDMD SKVSO PJRKZ DYFZJ XGSRR VTQYR WDARW DFVRK VDRKV TDFSZ ZDYFR DNNVO VTSXS AWVZR
%%%%%%%%%%%%%%%%%%% The output is %%%%%%%%%%%%%%%%%%%
11:R 10:D 9:V 6:S 5:Z 5:F 4:X 4:K 3:N 3:T 3:J 3:W 3:Y 2:O 2:U 2:A 1:Q 1:G 1:H 1:M 1:P Sorted in numeric descending order.
%%%%%%%%%%%%%%%%%%% What I'm working on %%%%%%%%%%%%%%%%%%%
To sort alpha after the numeric, and combine, so that the output will look something like this:
11R 10D 9V 6S 5FZ 4KX 3JNTWY 2AOU 1GHMPQ Then I will call it quits for awhile.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|