Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 将计数的点击数排序到单独的文件中_String_Text_Sh - Fatal编程技术网

String 将计数的点击数排序到单独的文件中

String 将计数的点击数排序到单独的文件中,string,text,sh,String,Text,Sh,我有short.txt(包含字符串)和long.txt(包含字符串) 例如short.txt包含: 这 那 long.txt包含: 这 这是 那 thisisan 这是一个例子 我有一个计算short.txt中的字符串是否在long.txt中的源代码 grep-F-o-F short.txt long.txt | sort | uniq-c | sort-nr>counted.txt 因此counted.txt将包含: 3这 1那 我的问题是:如何在单独的文件中获得结果,如: 3\u this.

我有short.txt(包含字符串)和long.txt(包含字符串)

例如short.txt包含:

long.txt包含: 这
这是

thisisan
这是一个例子

我有一个计算short.txt中的字符串是否在long.txt中的源代码

grep-F-o-F short.txt long.txt | sort | uniq-c | sort-nr>counted.txt

因此counted.txt将包含:
3这
1那

我的问题是:如何在单独的文件中获得结果,如:
3\u this.txt(因此点击次数+\u+word+.txt)
(其中包含) 这
这是
thisisan
这是一个示例
1_that.txt
(其中包含)

小列表可以包含10.000+个字符串,长列表为100.000.000+

我在玩.sh,因为我可以轻松地在MAC上运行它。不知道您是否有更快的解决方案。

long.txt将每月更新一次,而small.txt将每天更新一次。

所有这些都是一种迂回而低效的实现结果的方法,但是如果您已经完成了所有这些,并且创建了
counted.txt
,您可以使用它来创建所需的文件:

while read count word; do
  grep -F "$word" long.txt >"${count}_${word}.txt"
done < counted.txt
读取计数字时;做
grep-F“$word”long.txt>“${count}{word}.txt”
完成

但是您最好使用像Pulkit Agarwal的答案这样的一次性解决方案。

简单的Python解决方案。这并不是假设您已经创建了
counted.py


这个
发生了3次,不是4次谢谢你,它正在工作,是的,有点慢。问题是长文件大约2Gb,小文件大约5-10mb,我需要计算命中率,还需要查看匹配结果。不知道有没有可能做得更快,更好。我会使用
'{}{}.format(count,outfilename)
而不是
str()
和串联,但这只是风格品味。谢谢你的帮助。它跑得更快。如果您有更快的方法来检查10.000(短)和100.000.000(长)列表,请告诉我。
import os

with open('short.txt', 'r') as shorttxt:
    for s in shorttxt:
        outfilename = s[:-1] + '.txt'
        count = 0
        with open('long.txt', 'r') as longtxt, open(outfilename, 'w') as out:
            for l in longtxt:
                if s[:-1] in l:
                    count += 1
                    out.write(l)
        os.rename(outfilename, str(count) + '_' + outfilename)