Extract specific data from a text file

Viewed 2468

I have a txt file that appears in notepad++ like this:

/a/apple 1
/b/bat 10
/c/cat 22
/d/dog 33
/h/human/female 34

Now I want to extract everything after second slash before the numbers at the end. So the output I want is:

out = {'apple'; 'bat'; 'cat'; 'dog'; 'human/female'}

I wrote this code:

file= fopen('file.txt');
out=  textscan(file,'%s','Delimiter','\n');
fclose(file);

it gives:

out =
   {365×1 cell}

out{1} = 

    '/a/apple 1'
    '/b/bat 10'
    '/c/cat 22'
    '/d/dog 33'
    '/h/human/female 34'

How can I get the required output from the text file (directly if possible)? Or any regular expression if directly getting the required output is not possible?

3 Answers
Related