使用文件夹名称作为文本文件中的一列
懒惰的我正在考虑向一些文本文件添加一列。
文本文件位于目录中,我想将目录名称添加到文本文件中。
就像文件夹 the_peasant
中的文本文件 text.txt
has a wart
was dressed up like a witch
has a false nose
会变成
the_peasant has a wart
the_peasant was dressed up like a witch
the_peasant has a false nose
:然后我在其他名为“the_king”等的文件夹中有类似的文本文件。
我认为这是一个find 命令、bash 脚本和 sed 的组合,但我看不到它。有什么想法吗?
The lazy me is thinking about adding a column to some textfiles.
The textfiles are in directories and I would like to add the directory name to the text file.
Like the text file text.txt
in the folder the_peasant
:
has a wart
was dressed up like a witch
has a false nose
would become:
the_peasant has a wart
the_peasant was dressed up like a witch
the_peasant has a false nose
Then I have similar text files in other folders called "the_king" etc.
I would think this is a combination of the find command, bash scripting and sed but I cant see it through. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这可能对你有用:
或者如果你有 GNU sed:
This might work for you:
or if you have GNU sed:
目录树:
之前的目录和内容:
代码 (wart.py):
输出:
之后的目录和内容:
这很有趣!感谢您的挑战!
The directory tree:
Directories and contents before:
Code (wart.py):
Which outputs:
Directories and contents after:
This was fun! Thanks for the challenge!
简单的 python 脚本(显然,只要您将完整路径传递给目标文件,就应该在任何文件夹中工作):
Simple python script for this (should work from any folder, as long as you pass the fullpath to the target file, obviously):
这是我的想法:
这是一个如何构造命令的示例,假设存在以下文件:
首先
find /home/the_peasant -type f
将输出与上面完全相同的这些文件。接下来,sed 命令将输出一个文件名,后跟目录名,如下所示:
xargs 将每两行进行分组并将它们传递给 sh 命令,因此您最终会得到以下三个命令
:产生以下 sed 命令,该命令会将文件夹名称添加到每行的开头:
Here is what I came up with:
Here is an example of how the commands would be constructed, assuming the following files exist:
First
find /home/the_peasant -type f
would output those files exactly as above.Next, the sed command would output a file name, followed by the directory name, like this:
The xargs would group every two lines and pass them to the sh command, so you would end up with the following three commands:
And finally this will result in the following sed commands which will add the folder name to the beginning of each line:
使用 find 和 perl 的强制单行
假设 perl 和 sed 在您的 $PATH 中。生成一个 sed 命令文件来执行实际更改,以便您可以查看要执行的操作。
在我的测试中,该命令文件如下所示:
Obligatory single liner using find and perl
Assumes perl and sed are in your $PATH. Generates a file of sed commands to do the actual change so you can review what is to be done.
In my test, that command file looks like so:
我会的。
可以使用以下方式访问该目录
I would.
Accessing the directory can be done by using
您是否在适当的文件夹中运行脚本?然后你可以使用 os 模块来查找当前文件夹。假设您只想获取目录树的末尾,您可以使用 os.path,例如:
Are you running the script in the appropriate folder? Then you can use the os module to find the current folder. Say you wanted to take just the end of the directory tree, you could use os.path, like:
编辑:注意到有些东西不正确。
我删除了 dir 循环 - 它现在递归地行走。
抱歉搞混了。
使用 os.walk
输出:
Edit: noticed something wasn't correct.
I removed the dir loop - its recursively walking now.
Sorry for the mix up.
Using os.walk
Output:
这是 bash 和 awk 中的 one-ish-liner:
Here's a one-ish-liner in bash and awk: