[英]run linux cat command in python or bash loop
我在一个目录中有999,999个文件:
member.php\?1
member.php\?2
...
member.php\?99999
我想在特定文件的bash或python循环中运行以下cat
命令。 此命令应该从.php文件中提取用户名,但由于涉及大量文件而无法工作。
root@alimp5: cat member.php\?* | grep -i '<li class="navbit lastnavbit"><span>' | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt
我的解决方案(这种方式工作正常):在每个文件上逐步执行cat命令,如下所示:
root@alimp5:cat member.php\?1* | grep -i '<li class="navbit lastnavbit"><span>' | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt
root@alimp5:cat member.php\?2* | grep -i '<li class="navbit lastnavbit"><span>' | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt
...
root@alimp5:cat member.php\?9* | grep -i '<li class="navbit lastnavbit"><span>' | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt
我想通过在python循环或bash循环中运行它来自动执行上述解决方案。
例如(不起作用):
import os, sys
for count in range(1,10):
os.system('''cat member.php\?%d* | grep -i '<li class="navbit lastnavbit"><span>' | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt''') %count
有什么好办法呢?
#!/bin/bash
for i in {1...9999999}
do
$i
done
现在你只需在你假装的命令旁边添加$ i
'cat member.php\?$i
在你的python命令中,你做错了, % count
应该在os.system()
调用中。 示例 -
os.system('''cat member.php\?%d* | grep -i '<li class="navbit lastnavbit"><span>' | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt''' % count)
此外,您应该迭代到您想要的数量。
在Python中,我会使用glob
来扫描目录,以防文件数量发生变化。
import glob
for file in glob.glob('member*'):
process(file)
其中process是处理作为参数给出的文件的函数。
另外,请注意,如果您的php文件实际上是用HTML填充的,那么您可以使用BeautifulSoup来完成工作。
在bash中,我会使用find
:
find . -name member\*; -exec sh -c "grep -i '<li class=\"navbit lastnavbit\"><span>' | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt"
或者干脆
for file in member*; do
grep -i '<li class=\"navbit lastnavbit\"><span>' ${file} | cut -d'>' -f3 | cut -d'<' -f1 >> users.txt
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.