Forum Discussion

LZhang's avatar
LZhang
Frequent Contributor
17 years ago

How to exact a particular column from an external file

Hi, Ole:

I have a txt file named tc_list.txt, and it looks like this:

tc_name1, user1, tel1,
tc_name2, user2, tel2.
tc_name3, user3, tel3,
tc_name4, user4, tel4,
tc_name5, user5, tel5

It has 5 rows in total and each field is delimited by a comma.  I want to read in the file but extract and store only the FIRST column ("tc_name") to an array called list.  I tried using spliteachline(",") function but it didn't work.  Do you have a better way of doing that?


Thanks,
Li

1 Reply

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Li!

    please try something like

    def list = []

    new File("...").eachLine
    {
      line -> list.add( line.split( "," )[0] )
    }

    log.info( list )


    Hope this helps!

    regards,

    /Ole
    eviware.com