从文本开头的多个文件中查找行,然后用小写替换
我正在尝试查找包含以特定字符串开头的行的所有文件,然后在该行中设置所有内容,然后将其设置为小写。我能够做我想做的部分,但似乎无法弄清楚如何使一切正常工作。
这是我要寻找的字符串的一个示例:
_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用此
gnu-sed
与2个捕获组使用:\ l \ 2
将仅在第二个捕获组内容中较低。如上所述,这需要
gnu-sed
。如果您没有,则可以使用此awk
命令:You can use this
gnu-sed
with 2 capture groups:\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 thisawk
command:使用
sed
或
输出
Using
sed
or
Output
PERL解决方案:
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:
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. Addprint $_
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}
: replacePATTERN
withREPLACEMENT
..*
: 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 switchesperldoc perlre
: Perl regular expressions (regexes)