[英]Is it possible to work with 'for loop grep' commands?
I have lots of files in every year directory我每年目录中都有很多文件
and in each file have long and large sentence like this for exmaple在每个文件中都有像这样的又长又大的句子 for exmaple
home/2001/2001ab.txt
the AAAS kill every one not but me and you and etc
the A1CF maybe color of full fill zombie
home/2002/2002ab.txt
we maybe know some how what
home/2003/2003ab.txt
Mr, Miss boston, whatever
aaas will will will long long
and in home directory, I got home/reference.txt
(list of word file)在主目录中,我得到了home/reference.txt
(word 文件列表)
A1BG
A1CF
A2M
AAAS
I'd like to do count how many word in the file reference.txt
is in every single year file我想计算每个年份文件中reference.txt
文件中的单词数
this is my code where I run in every year directory home/2001/
, home/2002/
, home/2003/
这是我每年在目录home/2001/
、 home/2002/
、 home/2003/
中运行的代码
# awk
function search () {
awk -v pattern="$1" '$0 ~ pattern {print}' *.txt > $1
}
# load custom.txt
for i in $(cat reference.txt)
do
search $i
done
# word count
wc -l * > line-count.txt
this is my result这是我的结果
home/2001/A1BG
$cat A1BG
0
home/2001/A1CF
$cat A1CF
1
home/2001/A2M
$cat A2M
0
home/2001/AAAS
$cat AAAS
1
home/2001/line-count.txt
$cat line-count.txt
2021ab.txt 2
A1BG
A1CF 1
A2M 0
AAAS 1
result line-count.txt
file have all information what I want but I have to do this work repeat manually do cd directory
do run my code and then cd directory
I have around 500 directory and file, it is not easy结果line-count.txt
文件包含我想要的所有信息但我必须手动重复执行cd directory
执行我的代码然后cd directory
我有大约 500 个目录和文件,这并不容易
and second problem is wasty bunch of file create lots of file and takes too much time第二个问题是一堆浪费的文件会创建大量文件并花费太多时间
because of this at first I'd likt use grep
command but I dont' know how to use list of file instead of single word因此,一开始我会使用grep
命令,但我不知道如何使用文件列表而不是单个单词
that is why I use awk
这就是为什么我使用awk
How can i do it more simple我怎样才能做得更简单
at first I'd likt use grep command but I dont' know how to use list of file instead of single word起初我会 likt 使用 grep 命令,但我不知道如何使用文件列表而不是单个单词
You might use --file=FILE
option for that purpose, selected file should hold one pattern per line.您可以为此目的使用--file=FILE
选项,所选文件应每行包含一个模式。
How can i do it more simple我怎样才能做得更简单
You might use --count
option to avoid need of using wc -l
for that, consider following simple example, let file.txt
content be您可以使用--count
选项来避免为此使用wc -l
,请考虑以下简单示例,让file.txt
内容为
123
456
789
and file1.txt
content be和file1.txt
内容是
abc123
def456
and file2.txt
content be和file2.txt
内容是
ghi789
xyz000
and file3.txt
content be和file3.txt
内容是
xyz000
xyz000
then然后
grep --count --file=file.txt file1.txt file2.txt file3.txt
gives output给出 output
file1.txt:2
file2.txt:1
file3.txt:0
Observe that no files are created and file without matches does appear in output. Disclaimer : this solution assumes file.txt
does not contain character of special meaning for GNU grep
, if this does not hold do not use this solution.观察到没有创建任何文件,并且没有匹配的文件确实出现在 output 中。免责声明:此解决方案假定file.txt
不包含对 GNU grep
具有特殊含义的字符,如果这不成立,请不要使用此解决方案。
(tested in GNU grep 3.4) (在 GNU grep 3.4 中测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.