bash 如何在文件中的特定列中搜索 shell 中的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21205908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search a specific column in a file for a string in shell?
提问by achiever202
I am learning shell scripting. I wrote a script that indexes all the files and folders in the current working directory in the following tab separated format,
我正在学习 shell 脚本。我编写了一个脚本,以以下制表符分隔格式对当前工作目录中的所有文件和文件夹进行索引,
Path Filename File/Directory Last Modified date Size Textfile(Y/N)
路径文件名文件/目录上次修改日期大小文本文件(是/否)
for example, two lines in my file 'index.txt' are,
例如,我的文件“index.txt”中的两行是,
home/Desktop/C C d Thu Jan 16 01:23:57 PST 2014 4 KB N
home/Desktop/C/100novels.txt 100novels.txt f Thu Mar 14 06:04:06 PST 2013 0 KB Y
Now, i want to write another script to search for files from the previous file that takes in command line parameters, the file name. And also optional parameters as -dir (if to search only for directories), -date DATE (for last modified date), -t (for text files only) etc. How to go about this ?
现在,我想编写另一个脚本来从前一个文件中搜索文件,该文件接受命令行参数,即文件名。还有可选参数 -dir(如果只搜索目录)、-date DATE(最后修改日期)、-t(仅用于文本文件)等。如何解决这个问题?
I know the grep command that searches in a file for a string, but suppose i entered a file name, it would search the entire file for the file name instead of searching only the filename column. How do i do this? Is there any other command to achieve this ?
我知道在文件中搜索字符串的 grep 命令,但假设我输入了一个文件名,它会在整个文件中搜索文件名,而不是只搜索文件名列。我该怎么做呢?有没有其他命令可以实现这一点?
回答by glenn Hymanman
"i want to search in a speific column for a particular string... how to do that ?"
“我想在特定列中搜索特定字符串……怎么做?”
For example, to search for directories:
例如,要搜索目录:
awk -F '\t' ' == "d"' filename
You can also search by regular expression or substring matching. Please post further questions as you learn.
您还可以通过正则表达式或子字符串匹配进行搜索。请在学习时发布更多问题。
Single quotes prevent variable substitution (and other forms of expansion) -- see the bash manual. So you have to pass the value by other means:
单引号防止变量替换(和其他形式的扩展)——请参阅 bash 手册。所以你必须通过其他方式传递值:
awk -F'\t' -v s="$search_file" ' == s' ./index.txt > final.txt
or, use double quotes, more fragile and harder to read IMO
或者,使用双引号,更脆弱,更难阅读 IMO
awk -F'\t' "$2 == \"$search_file\"" ./index.txt > final.txt
Also, you don't mix single and double quotes. Pick one or the other, depending on the functionality you need.
此外,您不要混合使用单引号和双引号。根据您需要的功能选择一个或另一个。
回答by dacuna
You can doit using the command awk. For example to print the first column of a file using awk:
您可以使用命令 awk 来完成。例如,使用 awk 打印文件的第一列:
awk -F ":" '{print }' /etc/passwd
(note that the -F flag is to choose a field separator for the columns, in your case you don't need it). Then you can use grep to filter in that column. Good luck!
(请注意, -F 标志是为列选择字段分隔符,在您的情况下您不需要它)。然后您可以使用 grep 在该列中进行过滤。祝你好运!