Passing multiple value in a single argument in Perl.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing multiple value in a single argument in Perl.
# 1  
Old 12-30-2014
Passing multiple value in a single argument in Perl.

Hi all,

I have below code through which trying to pick data from specific columns strating from a certain row.

Code:
#!/usr/bin/perl
#This script is to pick the specific fields from a files starting from a specific row
# FILE -> Name of the file to be pasd at runtime.
# rn -> Number of the row from where the data has to be picked.
use strict;
use warnings;
my $file = shift || "FILE";
my $rn = shift;
my $cols = shift;
open( my $fh, "<", $file) or die "Could not open file '$file' : $!\n";
while (<$fh>) {
    $. <= $rn and next;
    my @fields = split(/\t/);
    print "$fields[$cols]\n";
}

My proble here is I am only able to get one column at a time. I want to fetch columns from
Code:
0, 1, 3..6, 21..33

but its giving me only first column.

I am runing below comand to execute the script.
Code:
perl extract.pl FILE 3 0, 1, 3..6, 21..33


Last edited by Abhisrajput; 12-30-2014 at 12:36 PM.. Reason: Missed the file name
# 2  
Old 12-30-2014
Try running it with
Code:
perl extract.pl 3 0,1,3..6,21..33

instead of with:
Code:
perl extract.pl 3 0, 1, 3..6, 21..33

# 3  
Old 12-30-2014
Quote:
Originally Posted by Don Cragun
Try running it with
Code:
perl extract.pl 3 0,1,3..6,21..33

instead of with:
Code:
perl extract.pl 3 0, 1, 3..6, 21..33

I have tried that way but it is not working.
Code:
perl extract.pl FILE 3 0, 1, 3..6, 21..33

It seems it taking the values as string. I need to convert it into integer. And having hard time to read it.
# 4  
Old 12-30-2014
I'm sorry that didn't work. I don't do much with perl, but I do know that when you invoke your script with:
Code:
perl extract.pl FILE 3 0, 1, 3..6, 21..33

all that your script is seeing when setting $cols with:
Code:
my $cols = shift;

is 0,. Getting rid of the spaces in the argument list or quoting the args as in:
Code:
perl extract.pl FILE 3 "0, 1, 3..6, 21..33"

would get rid of that problem, but neither of them will convert the string into a list of numbers.
# 5  
Old 12-31-2014
Hi.

This worked for me, assuming that the column string values are enclosed in quotes:
Code:
#!/usr/bin/perl
#This script is to pick the specific fields from a files starting from a specific row
# FILE -> Name of the file to be pasd at runtime.
# rn -> Number of the row from where the data has to be picked.
use strict;
use warnings;
my $file = shift || "FILE";
my $rn   = shift;
my $cols = shift;
open( my $fh, "<", $file ) or die "Could not open file '$file' : $!\n";
print "debug, reading columns :$cols: from $file at line $rn\n";

while (<$fh>) {
  $. <= $rn and next;
  my @fields = split(/\t/);
  # print "$fields[$cols]\n"; #Fails, need @, not $
  # print "@fields[0, 1, 3..6]\n"; #OK
  # print "@fields[0, 3]\n"; #OK
  # print "@fields[0,1,3..6,7]\n"; #OK
  # print @fields[$cols],"\n"; #Fails
  print join( "\t", @fields[ eval($cols) ] ), "\n";    #OK
}

Assuming this works for you, next time please include a short sample input and expected output. It's a pain for responders to create data files that may or may not match what you are working with.

Best wishes ... cheers, drl

Last edited by drl; 12-31-2014 at 02:05 PM..
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing a second argument

I am trying to pass a second argument like so: if ] then export ARG2=$2 else message "Second argument not specified: USAGE - $PROGRAM_NAME ARG1 ARG2" checkerror -e 2 -m "Please specify if it is a history or weekly (H or W) extract in the 2nd argument" fi however, it always goes... (4 Replies)
Discussion started by: MIA651
4 Replies

2. Shell Programming and Scripting

Tabbed multiple csv files into one single excel file with using shell script not perl

Hi Experts, I am querying backup status results for multiple databases and getting each and every database result in one csv file. so i need to combine all csv files in one excel file with separate tabs. I am not familiar with perl script so i am using shell script. Could anyone please... (4 Replies)
Discussion started by: ramakrk2
4 Replies

3. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

4. Shell Programming and Scripting

Help with passing argument

Hi, I have a script that is scheduled with cron and runs every night. The cron part looks like this: 00 20 * * 0,1,2,3,4,5,6 /usr/local/bin/BACKUP TBTARM HOT DELETE My issue is with the 3rd parameter. Somewhere in the script, i want to tell the script to delete some files if the 3rd... (7 Replies)
Discussion started by: dollypee
7 Replies

5. Shell Programming and Scripting

passing argument from one function to another

Hi all, In the given script code . I want to pass the maximum value that variable "i" will have in function DivideJobs () to variable $max of function SubmitCondorJob(). Any help? Thanks #!/bin/bash ... (55 Replies)
Discussion started by: nrjrasaxena
55 Replies

6. Programming

PERL:Combining multiple lines to single line

Hi All I need a small help for the below format in making a small script in Perl or Shell. I have a file in which a single line entries are broken into three line entries. Eg: I have a pen and notebook. All i want is to capture in a single line in a separate file. eg: I have a pen and... (4 Replies)
Discussion started by: Kalaiela
4 Replies

7. Shell Programming and Scripting

passing either of argument, not both in shell

Hi, I have a requirement to work on script, it should take either of arguments. wrote it as below. #!/bin/bash usage() { echo "$0: missing argument OR invalid option ! Usage : $0 -m|-r|-d } while getopts mrdvh opt; do case "$opt" in m) monitor_flag=monitor;;... (1 Reply)
Discussion started by: ramanaraoeee
1 Replies

8. Shell Programming and Scripting

Help with Passing argument and testing

Hi all First of all thanks for everyone to read by doubt.Am beginner in shell scripting Following are my doubts i have to pass an argument to shellscript how can i do that second i have to test the argument and shows error when nothing is passes third i have to match exact argument... (3 Replies)
Discussion started by: zeebala1981
3 Replies

9. UNIX for Dummies Questions & Answers

Using * when passing argument to alias

I have some folders with this structure: /data/me/a123xxx Where "xxx" is some changing series of letters, and "123" changes so folders might look like: /data/me/a123xxx /data/me/a234ysd /data/me/a534sds etc. The numbers are what matter to me (they identify the folder), so I created an... (7 Replies)
Discussion started by: ramparts
7 Replies

10. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies
Login or Register to Ask a Question