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.
CodePudding user response:
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
CodePudding user response:
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
CodePudding user response:
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)