Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am looking for a Perl script which does the following:
takes two options, like script.pl -option1 -option2
Option 1 has two choices, say choice1 and choice2 and I have shell code
to do both actions (which I hope I can port to Perl)
Here option 2 is the path and is optional. If it's not specified it uses the current directory
Desired script
checks arguments
if choice 1 :
go to path (arg2)
run a code (i have it ready)
If choice 2
go to path (arg2 )
run a code
(not tested)
#! /usr/bin/perl
use warnings;
use strict;
my ($action, $path) = #ARGV;
$path //= '.';
chdir $path or die "Cannot chdir to $path.\n";
my $script = {
choice1 => 'script1.sh',
choice2 => 'script2.sh',
}->{$action};
die "Invalid choice $action.\n" unless defined $script;
0 == system $script or die "Status: $?";
Run as script.pl choice1 /path.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am very new to perl, so don't have much knowledge in perl scripting.
I have two files test1.pl and testfinal.pl.
testfinal.pl
for loop{
$var = $_;
my $out = `perl test1.pl -p $var`;
}
test1.pl
Foo();
sub Foo(){
# I want to get $var from testfinal.pl so that I can perform some functions of that perticular varaible.
$elt = `mkdir $var`;
}
I checked some links, but I found for exporting I need to make the file in '.pm' format (testfinal.pm) which is not possible as I need to get the final output but executing testfinal.pl.
Can anyone help me here quickly.Please...
Pathak has covered some fine ways of passing your information through the file system, but I also note that you've passed $var through the command line. test1.pl should already have that info in #_, specifically as $_[1].
Examples:
Foo();
sub Foo(){
$elt = `mkdir $_[1]`;
}
or better
Foo($_[1]);
sub Foo{ #prototype deleted, probably should stay that way...
my $dir = shift;
$elt = `mkdir $dir`;
}
For cleaner handling of command line parameters, the GetOpt::Long module is core.
If you aren't attached to launching a shell & a 2nd instance of the perl executable, some other approches for running 2 files as a single program are the keywords use and require. (check perldoc for details.) These approaches allow you to share package variables or to directly pass parameters to the target subrutines.
I suggest that you create a module as documented in perlmod and Exporter.
The following demonstrates how to do this with your setup:
MyModule.pm:
package MyModule;
use strict;
use warnings;
use Exporter qw(import);
sub Foo {
my $var = shift;
print "mkdir $var\n";
}
testfinal.pl:
use strict;
use warnings;
use MyModule qw(Foo);
my #array = (...);
for my $var (#array) {
my $out = Foo($var);
}
Also note that perl has a native mkdir function, so there is no need to shell out to the system for that functionality.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is the data in my music.txt file
Seamus McGuire:The Wishing Tree:09-14-2000:14.95
Pat Kilbride:Loose Cannon:07-02-2000:15.95
Kevin Crawford:Seasons of Mists:06-23-2000:16.95
Prince:Purple Rain:01-01-1995:3.95
Meat Loaf:Bat out of Jell:03-03-1980:11.95
Eddie Money:Two Tickets:09-04-1979:8.98
I am trying to write a Unix Perl script that
Asks for artist's name (either first or last) from user
Displays artist's full name, CD title, date, and price in formatted
output.
Proper heading, with commands of your choice, is expected
can someone help me?
#!/usr/local/bin/perl
use strict;
use warnings;
print "Enter first or last name of artist: ";
chomp(my $input = <STDIN>); #Take input from user
open (my $fh, "<", "music.txt") or die $!; #Open the file in read mode
while(chomp(my $line = <$fh>)){
if($line =~ /$input/){ #Check if input matches with line
my #artist_info = split/:/,$line; #Split the data from line based on `:`
print " Name: $artist_info[0]\n CD Title: $artist_info[1]\n Date: $artist_info[2]\n Price: $artist_info[3]\n";
}
}
close($fh);
DEMO
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is part of Shell script.
This is to check disk Usage.
I want to know how this could convert to perl script ?
Please help me for this.
DFCMD="df -P"
for fs in `eval $DFCMD`; do
FS=`echo $fs | awk ' { print $1 } '`
done
Thanks
perl -le 'print /(\S+)/ for `df -P`'
There is probably a 1liner to do it, but I'd do this (un tested)
my #command = qw/df -P/;
open(my $in, "-|", #command) or die "Can't run `#command`: $!";
while(<$in>) {
my ($first_col) = split;
print "$first_col\n";
}
well, in perl, it is often use the system() function to execute the shell command, but the system() function can not return the output of the command.You can use backticks (``) to get the output. Use the out put, you can convert the shell script to perl script
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my #output = split('\r\n', `df -P | awk '{print \$1}'`);
6 print #output;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to remove every lines in a text file that contain the word "script" and then write whatever left into another file. But keep the originl file.
like this:
open the file
delete any line with the word "script"
then output whatever left after delete to another file.
perl -ne '/script/ or print' file > newfile
grep -v script original.file > new.file
Or if you really need perl:
#!/usr/bin/perl
use strict;
use warnings;
open(my $in, '<', 'input.txt')
or die "Cannot open input.txt: $!";
open(my $out, '>', 'output.txt')
or die "Cannot open output.txt: $!";
while (<$in>) {
print $out $_ unless /script/;
}
close($in);
close($out);
Finally, if you are only looking to match "script" if it is a word (and not part of a bigger string like "prescription" or "scripting") then change:
/script/
To:
/\bscript\b/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm writing a Perl script. If the text in a specified column (here column 13) matches a certain text namely 'one' or 'two', then the whole line (so all columns) should be copied to another file. My input is a tab-delimited .txt file.
This is what I have so far:
my $table1 = $ARGV[0];
open(my $variants,$table1) || die "$! $table1";
open(my $out,'>',"filtered.txt") || die "Can't write new file: $!";
while(<$variants>){
chomp;
my #line=split(/\t/); #split on tabs
if (($line[12] =~ m/one/) || ($line[12] =~ m/two/)){
print $out "$_";
}
}
Since I'm getting a 'use of uninitialized value' error, I wanted to know what needs to be changed in this code.
What is the problem?
perl -F'\t' -ane'print if $F[11]=~/one|two/' input > output
This is a great example of a program that should be written using the Unix filter model.
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
my #line = split /\t/;
# Do you mean 12? That's the 13th field
print if $line[12] =~ /one/ || $line[12] =~ /two/;
}
Simpler to write and easier to understand. Oh, and far more flexible (no hardcoded filenames).
Call it like this:
$ ./my_filter < input_file.txt > output_file.txt