0

I have an excel file which contains following values.I want to read those values from excel file and pass those values to execute my test.

users1=2

loop1=1

users2=1

loop2=1

Could you please anyone help how can i achieve that?

6
  • what operating system? also can you save excel as CSV? is there really "users1=2" in one cell? Commented Apr 8, 2015 at 18:52
  • linux.I am using shell script Commented Apr 8, 2015 at 18:53
  • Perl has several modules for reading and/or manipulating Excel files. Even if you want to keep the bulk of your product as a (non-Perl) shell script, you can have a Perl sub-script retrieve your values for you. Commented Apr 8, 2015 at 18:55
  • no i just want to read it from file and pass it to variable like users1=? etc etc Commented Apr 8, 2015 at 18:56
  • What version of Excel produced the file? Commented Apr 8, 2015 at 18:57

1 Answer 1

2

Using linux you have several choices, but none without using a script language and most likely installing an extra module.

Using Perl you could read Excel files i.e. with this module: https://metacpan.org/pod/Spreadsheet::Read

Using Python you might want to use: https://pypi.python.org/pypi/xlrd

And using Ruby you could go for: https://github.com/zdavatz/spreadsheet/blob/master/GUIDE.md

So whatever you prefer, there are tools to help you.

CSV Format

If you can get your data as CSV (comma separated values) file, then it is even easier, because no extra modules are needed.

For example in Perl, you could use Split function. Now that i roughly know the format of your CSV file, let me give you a simple sample:

#!/usr/bin/perl
use strict;
use warnings;

# put full path to your csv file here
my $file = "/Users/daniel/dev/perl/test.csv";

# open file and read data
open(my $data, '<', $file) or die "Could not read '$file' $!\n";

# loop through all lines of data
while (my $line = <$data>) {

  # one line
  chomp $line;

  # split fields from line by comma
  my @fields = split "," , $line;

  # get size of split array
  my $size = $#fields + 1;

  # loop through all fields in array
  for (my $i=0; $i < $size; $i++) {

    # first element should be user
    my $user = $fields[$i];
    print "User is $user";

    # now check if there is another field following
    if (++$i < $size) {

      # second field should be loop
      my $loop = $fields[$i];
      print ", Loop is $loop";

      # now here you can call your command
      # i used "echo" as test, replace it with whatever
      system("echo", $user, $loop);

    } else {
      # got only user but no loop
      print "NO LOOP FOR USER?";
    }
    print "\n";
  }
}

So this goes through all lines of your CSV file looks for User,Loop pairs and passes them to a system command. For this sample i used echo but you should replace this with your command.

Looks like i did you homework :D

Sign up to request clarification or add additional context in comments.

7 Comments

if you have CSV (comma separated values) format, then that is even easier. But CSV is not Excel!!! Excel can read and write CSV, but if you have data already there as CSV, then please post data here, so others can provide sample code, and change your question to CSV so it is not so misleading
usually there are no extra modules needed to read CSV, this can be done with simple native functions form either Perl, Python or Ruby
@user197501 added a simple CSV sample for you
i can use either of those so that's why i asked if it is easy with csv then i can use that
data will be same as i described in question Users1,1 loop1,1 Users2,2 loop2,1 and then in shell script i am passing those values to variables to execute my test.So i just simply want to read a file and then pass User1,loop1,Users2,loop2 values in my script.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.