如何更新"* .txt"的内容多个子目录中的文件 [英] How to update contents of a "*.txt" file in multiple sub directories

查看:108
本文介绍了如何更新"* .txt"的内容多个子目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录名称"technology",其中有多个子目录,每个子目录至少有一个* .txt文件.

I have a directory name "technology" that have multiple sub directories in it and each sub directory have at least one *.txt file.

每个"* .txt"文件都有一行以单词"how_to_repeat"开头的行,此后它可以有任何其他字符串值,但内容仅在一行.

Every "*.txt" file have one line that starts with word "how_to_repeat" and after that it could have any other string value but it contents will only be at one line.

我正在使用find找出其中具有"how_to_repeat"值的文件,如下所示.

I am using find to find out files that have "how_to_repeat" values in it as shown below.

find . -name '*.txt' -exec grep -H 'how_to_repeat' {} \; |wc -l
127

现在, 我的问题是我如何更新每个"* .txt"文件的内容,以使它以恒定字符串(即"how_to_repeat =运行测试数据")替换以"how_to_repeat"单词开头的整行.

Now, My question is how can i update the contents of every "*.txt" file such that it replaces the complete line that starts with "how_to_repeat" word with constant string i.e. "how_to_repeat= Run testing data".

Perl脚本正在运行,但不能完整显示

Perl script is working but not for the the full line

perl -p -i -e 's/how_to_repeat /how_to_repeat= Run testing data  /'   `find ./ -name *.txt`

想知道如何替换整行.

Wondering how it can replace the full line.

推荐答案

要匹配到行尾,只需在匹配表达式的末尾添加.*:

To match until the end of the line, you just need to add .* at the end of the matching expression:

s/how_to_repeat .*/how_to_repeat= Run testing data /

因此,正如您在注释中确认的那样,整个命令变为:

So the whole command, as you confirmed in the comments, becomes:

perl -p -i -e 's/how_to_repeat .*/how_to_repeat= Run testing data  /'   `find ./ -name *.txt`

这篇关于如何更新"* .txt"的内容多个子目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆