18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to retrieve a specific line from a file

PriyaPriya Members
edited November -1 in Sahi - Open Source
hi ,
can any one help me , how to read a single or specific line from a file.
i'am using _readFile(" path.txt");
but it is reading the complete data .

Comments

  • Hi Priya,

    _readFile will read all the contents of the file into the variable designated by you. You need to use pattern matching/regular expression capabilities of javascript to extract the required line from your file. If possible you can post the content your text file and the required text to be extracted.

    Regards,
    Pankaj.
  • hi pankaj,
    Thanks for the response,
    my file consists of Username,Qualification,Age which is stored in d "D:\\details.txt".
    now how can i retrieve the Qualification from that file.
  • hi,
    can you send in what format it is available in the file. i mean how the data is seperated in the text file. its comma separated, tab delimited etc.
    any more information will help.
  • Hi pankaj,

    The file is written like this:

    name,Qualification,Age
    user1,B.sc,21
    User2,M.sc,22
    user3,B.tech,23
  • pankaj.nithpankaj.nith Members
    edited November 2008
    Hi Priya,

    Here is a function to retrieve text from a file using regular expression. It is not a very fine piece of code (Didn't have much time). Its just to give you a direction to think upon.
    Still finding it difficult to use this to set a variable to the value returned by the function. It throws some error. I used older version of sahi for this (non-rhino).
    I hope Narayan can throw some light why it throws error if we try to set a variable to the value returned by the function below. I observed this behavior when ever I try to user regular expression functions like match(), substring() etc.
    function match(){
    var $content = _readFile("C:\\sahi\\scripts\\test.txt");
        var $lines = new Array();
        $lines = $content.split("\n");
        for (var $i = 0; $i < $lines.length; $i++){
            if ($lines[$i].match(/user2/))
                return $lines[$i].match(/,\D*,/i);
        }
        return "Not Found";    
    }
    
    Below is the text file i used;
    name,Qualification,Age
    user1,B.sc,21
    user2,M.sc,22
    user3,B.tech,23
    
    This one searches for the qualification of the user2 only but its returning it with the surrounding commas as well. you will have to biuld a more refined regular expression for your purpose.
    It will need to be worked upon to make it work for you but I hope that will help you somewhat :)

    Regards,
    Pankaj.
  • pankaj.nithpankaj.nith Members
    edited November 2008
    This is the log i get if I call this function in an _alert like this :
    _alert(match());
    Starting script
    _sahi.handleSet('\$qual' + 1, match());
    _alert(,M.sc,); SyntaxError: syntax error
    ()@:0
    eval("_sahi._alert(,M.sc,);")@:0
    ("_sahi._alert(,M.sc,);")@http://xyz/_s_/dyn/Player_script/script.js:2
    ()@http://xyz/_s_/spr/concat.js:2379
    @http://xyz/_s_/spr/concat.js:2293
    Stopping script
    
  • pankaj.nithpankaj.nith Members
    edited November 2008
    Hi,
    This piece of code will return you the qualification of the username passed to the function match() as long as the format of the text file is that provided in my previous message.
    function match($user){
    var $content = _readFile("C:\\sahi\\scripts\\test.txt");
        var $lines = new Array();
        $lines = $content.split("\n");
        for (var $i = 0; $i < $lines.length; $i++){
            if ($lines[$i].match($user)){
                var $x = $lines[$i].split(",");
                return $x[1];
            }
            
        }
    return "Not Found";    
    }
    
    var $qual;
    _set($qual, match("user2"));
    _alert($qual);
    
    Regards,
    Pankaj.
  • i have run my script as your above explanation but i got error in log file that i mention below
    _sahi.setServerVar('\$qual', match("user2"));
    ReferenceError: match is not defined

    Anybody have solution ?
  • narayannarayan Administrators
    Hi Paresh,

    This is an old post from 2008. Please create a new post, tell us what your requirement is, what you tried and what failed.

    If you have not already done so, please read the posting guidelines: http://sahi.co.in/forums/viewtopic.php?id=1640

    Regards,
    Narayan
This discussion has been closed.