简体   繁体   English

在目录中搜索文本文件,并在每个文本文件后附加(静态)行

[英]Search for text files in a directory and append a (static) line to each of them

I have a directory with many subdirectories and files with suffixes in those subdirectories (eg FileA-suffixA FileB-SuffixB FileC-SuffixC FileD-SuffixA , etc). 我有一个目录,其中包含许多子目录和这些子目录中带有后缀的文件(例如FileA-suffixA FileB-SuffixB FileC-SuffixC FileD-SuffixA等)。

How can I recursively search for files with a certain suffix, and append a user-defined line of text to those files? 如何递归搜索具有特定后缀的文件,并将用户定义的文本行附加到这些文件? I feel like this is a job for grep and sed, but I'm not sure how I would go about doing it. 我觉得这是grep和sed的工作,但我不确定如何去做。 I'm fairly new to scripting, so please bear with me. 我对脚本还很陌生,所以请多多包涵。

You can do it like 你可以做到

find /where/to/search -type f -iname '*.SUFFIX' -exec echo "USER DEFINED STRING" >> \{\} \;
  1. find searches in the suplied path 在提供的路径中find搜索
  2. -type f finds only files -type f仅查找文件
  3. -iname '*.SUFFIX' find the .SUFFIXed names, case ignored -iname '*.SUFFIX'查找.SUFFIXed名称,忽略大小写
find ./ -name "*suffix" -exec bash -c 'echo "line_to_add" >> $1' -- {} \;

Basically you use find to get a list of the files. 基本上,您使用find来获取文件列表。 Then you use bash to echo append your line to that list. 然后,使用bash回显将行添加到该列表。

暂无
暂无

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

相关问题 将目录名称附加到文件末尾并将其移动 - Append directory name to the end of the files and move them 搜索并替换linux目录的所有文件中的文本 - Search and replace text in all files of a linux directory 如何将主目录列表附加到文本文件以及如何移动文件? - How to append list of home directory to a text file and how to move files? 在linux系统上找到所有与'name'匹配的文件,并用它们搜索'text' - Find all files matching 'name' on linux system, and search with them for 'text' 遍历文件目录并将每一行读入数组 - Iterating through a directory of files and reading each line into an array 如何从目录中递归找到的所有文件中搜索行匹配 - how to search for a line match from all files recursively found in a directory UNIX \\ LINUX:如何向文件内的每一行添加目录文本? - UNIX\LINUX: How to add a directory text to each line inside a file? 逐行读取文件->搜索另一个文本文档中的每一行,如果匹配,则输出到另一个文本文件 - Read a file line by line -> search for each line in another text document, if match, output to another text file Linux:逗号分隔文本:阅读最后两个,添加它们,将它们放在每行的末尾 - Linux: comma-delimited text: read last two, add them, place them at end of each line 从子文件中的母文件中搜索编号,并将子文件中的整行附加回母文件中 - Search a number from mother files in daughter file and append the complete line from daughter file back in mother file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM