cancel
Showing results for 
Search instead for 
Did you mean: 

How to exact a particular column from an external file

Frequent Contributor

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 1
Super Contributor

Re: How to exact a particular column from an external file

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
New Here?
Join us and watch the welcome video:
Watch the New Interview
7Mistakes
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win