简体   繁体   English

将文本文件中文件名的一部分与目录中的文件名进行比较(grep + awk)

[英]Comparing part of a filename from a text file to filenames from a directory (grep + awk)

This is not exactly the easiest one to explain in a title. 这并不是标题中最简单的解释。

I have a file inputfile.txt that contains parts of filenames: 我有一个文件inputfile.txt ,其中包含部分文件名:

file1.abc
filed.def
fileq.lmn

This file is an input file that I need to use to find the full filenames of an actual directory. 该文件是一个输入文件,我需要使用它来查找实际目录的完整文件名。 The ends of the filenames are different from case to case, but part of them is always the same. 文件名的结尾因大小写而异,但其中一部分始终相同。

I figured that I could grep text from the input file to the ls command in said directory (or the ls command to a simple text file), and then use awk to output my full desired result, but I'm having some trouble doing that. 我认为我可以将输入文件中的文本grep到上述目录中的ls命令(或ls命令中的文本文件),然后使用awk输出我想要的全部结果,但是这样做有点麻烦。

file1.abc is read from the input file inputfile.txt It's checked against the directory contents. 从输入文件inputfile.txt读取file1.abc并对照目录内容进行了检查。 If the file exists, specific directories based on the filename are created. 如果文件存在,则将基于文件名创建特定目录。

(I'm also in a Busybox environment.. I don't have a lot at my disposal) (我也处于Busybox环境中。我没有太多可支配的东西)

Something like this... 像这样

cat lscommandoutput.txt \
        | awk -F: '{print("mkdir" system("grep $0"); inputfile.txt}' \
        | /bin/sh

Thank you. 谢谢。

Edit: My apologies for not being clear on this. 编辑:我对此表示不清楚。 The output should be the full filename of each line found in lscommandoutput.txt using the inputfile.txt to grep those specific lines. 输出应该是在lscommandoutput.txt中找到的每一行的完整文件名,并使用inputfile.txt对这些特定行进行grep。

If inputfile.txt contains: 如果inputfile.txt包含:

file1.abc
filed.def
fileq.lmn

and lscommandoutput.txt contains: lscommandoutput.txt包含:

file0.oba.ca-1.fil
file1.abc.de-1.fil
filed.def.com-2.fil
fileh.jkl.open-1.fil
fileq.lmn.he-2.fil

The extra lines that aren't contained in the inputfile.txt are ignored. inputfile.txt中未包含的多余行将被忽略。 The ones that are in the inputfile.txt have a directory created for them with the name that got grepped from lscommandoutput.txt. inputfile.txt中的文件具有为其创建的目录,该目录的名称是从lscommandoutput.txt中获取的。

/dir/dir2/file1.abc.de-1.fil/ <-- directory in which files can be placed in
/dir/dir2/filed.def.com-2.fil/
/dir/dir2/fileq.lmn.he-2.fil/

Hopefully that is a little bit clearer. 希望这一点更加清楚。

First, you win a useless use of cat award 首先,您会获得cat无用使用

Secondly, you've explained this really badly. 其次,您已经对此做出了非常糟糕的解释。 If you can't describe the problem clearly in plain English it's not surprising you are having trouble turning it into a script or set of commands. 如果您不能用简单的英语清楚地描述问题,那么将其转换为脚本或命令集就不会感到奇怪。

grep -f is a good way to get the directory names, but I don't understand what you want to do with them afterwards. grep -f是获取目录名称的好方法,但是我不明白之后您要如何处理它们。

My problem now is using the outputted file with the one file I want to put the folders 我现在的问题是将输出文件与我要放置文件夹的一个文件一起使用

Wut? What does "the one file I want to put the folders" mean? “我要放入文件夹的一个文件”是什么意思? Where does the file come from? 文件来自哪里? Is it the file named in inputlist.txt ? 它是在inputlist.txt命名的文件吗? Does it go in the directory that it matched? 它是否位于匹配的目录中?

If you just want to create the directories you can do: 如果只想创建目录,则可以执行以下操作:

fgrep -f ./inputfile.txt ./lscommandoutput.txt | xargs mkdir

NB you probably want fgrep so that the input strings aren't treated as regular expressions and regex metacharacters such as . 注意,您可能希望使用fgrep这样输入字符串就不会被视为正则表达式和regex元字符,例如. are ignored. 被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM