如何查找包含字符串"abcd"的文件名使用awk从目录中的多个文件中的文件内容中 [英] how to find a file name that contains a string "abcd " in the file content from multiple files in the directory using awk
本文介绍了如何查找包含字符串"abcd"的文件名使用awk从目录中的多个文件中的文件内容中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
"bookrecords"目录包含多个文件
the "bookrecords" directory has multiple files
bookrecords
1.txt
2.txt
3.txt .....
文件2.txt具有内容
file 2.txt has the content
2.txt
author: abcd
title: efg
year: 1980
如何通过使用author:作为关键字来获取文件名2.txt
how can I get the file name 2.txt by using author: as a keyword
使用awk命令
我尝试使用grep命令,但我想使用awk命令
I try to use grep command but I want to use awk command
SearchRecord()
{
name = abcd
total=`cat $bookrecords | grep -cwi $name`
record=`cat $bookrecords | grep -wi $name`
menu
}
推荐答案
和awk
$ awk -v search_string="$name" '$0~search_string{print FILENAME; exit}' bookrecords/*
但是,如果您不进行结构化搜索,我认为grep
会更好
however, I think grep
is better if you're not structurally searching
$ grep -lF "$name" bookrecords/*
这篇关于如何查找包含字符串"abcd"的文件名使用awk从目录中的多个文件中的文件内容中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文