In addition to Daniel, answer , you also seem to be asking for a list of files where the word occurs only once. To do this, I continue to run FindList
for all files
res =FindList[filelist, "love"]
Then reduce the results to single rows using
lines = Select[ res, Length[
But this does not exclude cases when there is more than one case in one line. For this, you can use StringCount
and only accept instances where it is 1, as follows
Select[ lines, StringCount[
RegularExpression
indicates that โloveโ should be a single word using the word boundary marker ( \\b
), so words such as โbeautifulโ will not be included.
Change It seems that FindList
returns a flattened list when transferring a list of files, so you cannot determine which element will work with which file. For example, if you have 3 files, and they contain the word "love", 0, 1 and 2 times, respectively, you will get a list that would look like
{, love, love, love }
which is clearly not useful. To overcome this, you will have to process each file individually, and this is best done using Map
( /@
), as follows
res = FindList[#, "love"]& /@ filelist
and the rest of the code above works as expected.
But, if you want to associate the results with the file name, you need to change it a little.
res = {#, FindList[#, "love"]}& /@ filelist lines = Select[res, Length[ #[[2]] ] ==1 && (* <-- Note the use of [[2]] *) StringCount[ #[[2]], RegularExpression[ "\\blove\\b" ] ] == 1& ]
which returns a list of form
{ {filename, { "string with love in it" }, {filename, { "string with love in it" }, ...}
To extract the file names, simply enter lines[[All, 1]]
.
Note that for Select
for the properties you wanted, I used Part
( [[ ]]
) to indicate the second element at each reference point, as well as to extract file names.