gpt4 book ai didi

linux - 选择文件夹中的特定文件

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:24 26 4
gpt4 key购买 nike

我想在目录中的文件列表中选择文件的特定格式。请查找以下示例。

我有以下文件列表(6 个文件)。

套装1
1) MAG_L_NT_AA_SUM_2017_01_20.dat
2) MAG_L_NT_AA_2017_01_20.dat

第二组
1) MAG_L_NT_BB_SUM_2017_01_20.dat
2) MAG_L_NT_BB_2017_01_20.dat

Set-3
1) MAG_L_NT_CC_SUM_2017_01_20.dat
2) MAG_L_NT_CC_2017_01_20.dat

从上述三组中,我只需要 3 个文件。
1) MAG_L_NT_AA_2017_01_20.dat
2) MAG_L_NT_BB_2017_01_20.dat
3) MAG_L_NT_CC_2017_01_20.dat

注意:可以有多行命令,因为我已经为上述要求创建了脚本。谢谢

最佳答案

解决您的问题的最简单且最不复杂的解决方案可能是结合 find (用于在目录层次结构中搜索文件的工具)和 grep (用于打印与模式匹配的线条的工具)。您还可以通过输入 man findman grep 来阅读这些工具手册。

在直接解决问题之前,我们需要了解我们将如何解决您的问题。要在我们搜索的文件名中查找模式,我们将使用带有选项 -namefind 命令:

-name pattern

Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters ('*', '?', and '[]') match a '.' at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CONFORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell.

例如,如果我们要在名为'words_directory'的目录中搜索包含字符串'abc'的文件,我们将输入以下内容:

$ find words_directory -name "*abc*"

如果我们想搜索目录中的所有目录:

$ find words_directory/* -name "*abc*"

首先,我们需要查找以字符串 "MAG_L_NT_" 开头、以 ".dat" 结尾的所有文件,因此要在 /your/specified/path/ 中查找所有匹配的名称,其中包含许多子目录,这些子目录可能包含与此模式匹配的文件:

$ find /your/specified/path/* -name "MAG_L_NT_*.dat"

然而,这会打印所有找到的文件名,但我们仍然得到包含 "SUM" 字符串的名称,这是 grep 中的。要排除包含不需要的字符串的名称,我们将使用选项 -v:

-v, --invert-match

Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX .)

要使用 grep 过滤掉第一个命令输出,我们将使用 pipe () |:

The standard shell syntax for pipelines is to list multiple commands, separated by vertical bars ("pipes" in common Unix verbiage). For example, to list files in the current directory (ls), retain only the lines of ls output containing the string "key" (grep), and view the result in a scrolling page (less), a user types the following into the command line of a terminal:

ls -l | grep key | less

"ls -l" produces a process, the output (stdout) of which is piped to the input (stdin) of the process for "grep key"; and likewise for the process for "less". Each process takes input from the previous process and produces output for the next process via standard streams. Each "|" tells the shell to connect the standard output of the command on the left to the standard input of the command on the right by an inter-process communication mechanism called an (anonymous) pipe, implemented in the operating system. Pipes are unidirectional; data flows through the pipeline from left to right.

 process1 | process2 | process3

熟悉上述用于实现目标的命令和选项后,您就可以准备解决方案了:

$ find /your/specified/path/* -name "MAG_L_NT_*.dat" | grep -v "SUM"

此命令将生成以“MAG_L_NT_”开头并以“.dat”结尾的所有名称的输出。 grep -v 将使用第一个命令输出作为输入,并删除所有包含 "SUM" 字符串的行。

关于linux - 选择文件夹中的特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47675490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com