从文本开头的多个文件中查找行,然后用小写替换

发布于 2025-01-28 21:06:38 字数 467 浏览 6 评论 0原文

我正在尝试查找包含以特定字符串开头的行的所有文件,然后在该行中设置所有内容,然后将其设置为小写。我能够做我想做的部分,但似乎无法弄清楚如何使一切正常工作。

这是我要寻找的字符串的一个示例:

      _assetBundleName: SomeDirectory/ChildDirectory

我需要它:

      _assetBundleName: somedirectory/childdirectory

所以我无法将整个行转换为较低的情况,只是在我要寻找的字符串之后的所有内容(这是_assetBundlename:)。而且我需要在许多文件(从运行命令的所有目录和子目录中)执行此操作。

sed的s/[az]/\ l&/g'将所有内容转换为小写,而不仅仅是我找到的字符串之后的所有内容。

I'm trying to find all files that contains lines that start with a specific string, and then set everything in that line after that to lowercase. I'm able to do parts of what I want but can't seem to figure out how to get it all to work.

Here's an example of the string I'm looking for:

      _assetBundleName: SomeDirectory/ChildDirectory

and I need it to be:

      _assetBundleName: somedirectory/childdirectory

So I can't convert the entire line to lower case, just everything after the string I'm looking for (which is _assetBundleName:). And I need to perform this on many files (all directories and subdirectories from where the command is run).

sed 's/[a-z]/\L&/g' converts everything to lowercase, not just everything after the string I've found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话里做英雄 2025-02-04 21:06:39

您可以使用此gnu-sed与2个捕获组使用:

sed -E 's/(_assetBundleName:)(.*)/\1\L\2/' file

\ l \ 2将仅在第二个捕获组内容中较低。


如上所述,这需要gnu-sed。如果您没有,则可以使用此awk命令:

awk 'BEGIN {FS=OFS=":"} 
$1 ~ /_assetBundleName/ {$2 = tolower($2)} 1' file

You can use this gnu-sed with 2 capture groups:

sed -E 's/(_assetBundleName:)(.*)/\1\L\2/' file

\L\2 will lowercase only the 2nd capture group content.


As noted above that this requires gnu-sed. If you don't have that then you can use this awk command:

awk 'BEGIN {FS=OFS=":"} 
$1 ~ /_assetBundleName/ {$2 = tolower($2)} 1' file
嗼ふ静 2025-02-04 21:06:39

使用sed

$ sed '/_assetBundleName:/s/:.*/\L&/' input_file

$ sed "/_assetBundleName:/s/\(.*\)\(:.*\)/echo '\1'\$(echo \\2 | tr 'A-Z' 'a-z')/" file | bash

输出

 _assetBundleName: somedirectory/childdirectory

Using sed

$ sed '/_assetBundleName:/s/:.*/\L&/' input_file

or

$ sed "/_assetBundleName:/s/\(.*\)\(:.*\)/echo '\1'\$(echo \\2 | tr 'A-Z' 'a-z')/" file | bash

Output

 _assetBundleName: somedirectory/childdirectory
把梦留给海 2025-02-04 21:06:39

PERL解决方案:

perl -i -pe 's{(_assetBundleName:)(.*)}{$1\L$2}' file1 file2 ...

Perl单线使用以下命令行标志:
-e:告诉Perl在线寻找代码,而不是在文件中。
-p:一次在输入一行上循环,将其分配给$ _默认情况下。添加打印$ _在每个循环迭代后。
-i:编辑就地编辑输入文件(覆盖输入文件)。
-i.bak:在现场编辑输入文件(覆盖输入文件)。在覆盖之前,通过将其名称附加到Extension .bak来保存原始文件的备份副本。

s {模式} {replacement}:替换模式替换>替换
。*:重复0次或更多次的任何字符。
$ 1,$ 2:第一个和第二个捕获组,用括号捕获。
\ l $ 2:第二个捕获组中的所有内容。

另请参见:
perldoc perlrun
perldoc perlre:perl Priorder Expressions(Regexes)

A Perl solution:

perl -i -pe 's{(_assetBundleName:)(.*)}{$1\L$2}' file1 file2 ...

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-i : Edit input files in-place (overwrite the input file).
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.

s{PATTERN}{REPLACEMENT} : replace PATTERN with REPLACEMENT.
.* : any character, repeated 0 or more times.
$1, $2: first and second capture groups, captured with parentheses.
\L$2 : lowercase everything in the second capture group.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlre: Perl regular expressions (regexes)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文