Extract specific data from a text file

Likeunknown

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?

gnovice

You can get the desired output directly from textscan, without any further processing needed:

file = fopen('file.txt');
out = textscan(file, '/%c/%s %d');
fclose(file);
out = out{2}

out =

  5×1 cell array

    'apple'
    'bat'
    'cat'
    'dog'
    'human/female'

Note that the two slashes in the format specifier string will be treated as literal text to ignore in the output. Any additional slashes will be captured in the string (%s). Also, it is unnecessary to specify a delimiter argument since the default delimiter is whitespace, so the trailing number will be captured as a separate numeric value (%d).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract specific data from text

How to extract specific data from a text file in Python?

Extract specific records from a text file

Python to extract specific numbers from text file

How to extract a specific text from gz file?

Extract specific strings from text file

Extract specific columns from text file

Extract a value from a text file in a specific position

Extract data from txt file and create new file based on specific text dynamically

Extract Specific Data from Txt file python

Extract Specific Data From File in Python

How extract extract specific text from pdf file - python

How to extract specific data from a text file and write into CSV using python

Extract data from text file VBA, where the line contains a specific string

Search for specific strings of data from a binary .dat file, only extract text

Extract data between a pattern from a text file

Extract Data from Text File into Excel

Extract data from Text File into Excel in vba

Extract data from text file in PowerShell

Extract Specific Text from a Text File Using C#

How to extract a specific text from text file in c#

Extract text from a specific pattern in text file using python

Batch file - extract specific text from .txt file

Reading specific data from a text file in python

Parse specific data from a text file

How to get specific data from text file

Deleting a specific data from a text file in Python

Extract Specific word from Text file between Specifc words in BASH

Extract specific information from text file using PHP