Replace string with spaces in entire directory
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I want to replace 'Aux Power [A]' in all files to 'aux_power'.
Only the files with 'Aux Power [A]' can be modified, the rest need to remain untouched.
I have tried
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/'Aux Power [A]'/'aux_power_w'/g"
but it doesn't seem to work.
Please advice.
text-processing sed grep find
add a comment |Â
up vote
0
down vote
favorite
I want to replace 'Aux Power [A]' in all files to 'aux_power'.
Only the files with 'Aux Power [A]' can be modified, the rest need to remain untouched.
I have tried
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/'Aux Power [A]'/'aux_power_w'/g"
but it doesn't seem to work.
Please advice.
text-processing sed grep find
1
Just to clarify: You wish to change file contents, do you?
â Ned64
Feb 17 at 11:11
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to replace 'Aux Power [A]' in all files to 'aux_power'.
Only the files with 'Aux Power [A]' can be modified, the rest need to remain untouched.
I have tried
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/'Aux Power [A]'/'aux_power_w'/g"
but it doesn't seem to work.
Please advice.
text-processing sed grep find
I want to replace 'Aux Power [A]' in all files to 'aux_power'.
Only the files with 'Aux Power [A]' can be modified, the rest need to remain untouched.
I have tried
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/'Aux Power [A]'/'aux_power_w'/g"
but it doesn't seem to work.
Please advice.
text-processing sed grep find
edited Feb 17 at 12:13
Jeff Schaller
31.2k846105
31.2k846105
asked Feb 17 at 10:51
Umer
1
1
1
Just to clarify: You wish to change file contents, do you?
â Ned64
Feb 17 at 11:11
add a comment |Â
1
Just to clarify: You wish to change file contents, do you?
â Ned64
Feb 17 at 11:11
1
1
Just to clarify: You wish to change file contents, do you?
â Ned64
Feb 17 at 11:11
Just to clarify: You wish to change file contents, do you?
â Ned64
Feb 17 at 11:11
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
The issue with your code is twofold:
The
grep
command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([
and]
). You have the same issue in yoursed
substitution.Filenames outputted by
grep -l
may be mangled. If a file contains an unprintable character (such as newline),grep
will simply drop that from the outputted name. This means thatsed
won't be invoked on that file.
This is assuming you'd like to replace the string within the file content, not in filenames:
To find all files beneath the directory /mnt/d/power/jan/output
that contains the exact string Aux Power [A]
, you would use
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';' -print
To change the string to aux_power
you would extend that command like so:
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';'
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This is assuming GNU sed
.
The grep -Fq
will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F
is used).
The sed
command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).
If you'd like to get the names of the modified files listed in the terminal, then leave the -print
in from the first find
command, after the -exec grep ... ';'
.
Since the sed
expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to
find /mnt/d/power/jan/output -type f
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This applies the substitution to all files under the /mnt/d/power/jan/output
directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).
You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
â Gerard H. Pille
Feb 17 at 17:38
@GerardH.Pille Please explain and I will get it right.
â Kusalananda
Feb 17 at 17:50
@GerardH.Pille If you referring to the stringaux_power_w
, then all I can say is that the question asks to replace withaux_power
while his attempt is to replace withaux_power_w
. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
â Kusalananda
Feb 17 at 18:13
add a comment |Â
up vote
1
down vote
You should escape the square brackets for grep. "[A]" is the same as "A".
Try
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The issue with your code is twofold:
The
grep
command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([
and]
). You have the same issue in yoursed
substitution.Filenames outputted by
grep -l
may be mangled. If a file contains an unprintable character (such as newline),grep
will simply drop that from the outputted name. This means thatsed
won't be invoked on that file.
This is assuming you'd like to replace the string within the file content, not in filenames:
To find all files beneath the directory /mnt/d/power/jan/output
that contains the exact string Aux Power [A]
, you would use
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';' -print
To change the string to aux_power
you would extend that command like so:
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';'
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This is assuming GNU sed
.
The grep -Fq
will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F
is used).
The sed
command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).
If you'd like to get the names of the modified files listed in the terminal, then leave the -print
in from the first find
command, after the -exec grep ... ';'
.
Since the sed
expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to
find /mnt/d/power/jan/output -type f
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This applies the substitution to all files under the /mnt/d/power/jan/output
directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).
You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
â Gerard H. Pille
Feb 17 at 17:38
@GerardH.Pille Please explain and I will get it right.
â Kusalananda
Feb 17 at 17:50
@GerardH.Pille If you referring to the stringaux_power_w
, then all I can say is that the question asks to replace withaux_power
while his attempt is to replace withaux_power_w
. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
â Kusalananda
Feb 17 at 18:13
add a comment |Â
up vote
2
down vote
The issue with your code is twofold:
The
grep
command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([
and]
). You have the same issue in yoursed
substitution.Filenames outputted by
grep -l
may be mangled. If a file contains an unprintable character (such as newline),grep
will simply drop that from the outputted name. This means thatsed
won't be invoked on that file.
This is assuming you'd like to replace the string within the file content, not in filenames:
To find all files beneath the directory /mnt/d/power/jan/output
that contains the exact string Aux Power [A]
, you would use
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';' -print
To change the string to aux_power
you would extend that command like so:
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';'
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This is assuming GNU sed
.
The grep -Fq
will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F
is used).
The sed
command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).
If you'd like to get the names of the modified files listed in the terminal, then leave the -print
in from the first find
command, after the -exec grep ... ';'
.
Since the sed
expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to
find /mnt/d/power/jan/output -type f
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This applies the substitution to all files under the /mnt/d/power/jan/output
directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).
You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
â Gerard H. Pille
Feb 17 at 17:38
@GerardH.Pille Please explain and I will get it right.
â Kusalananda
Feb 17 at 17:50
@GerardH.Pille If you referring to the stringaux_power_w
, then all I can say is that the question asks to replace withaux_power
while his attempt is to replace withaux_power_w
. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
â Kusalananda
Feb 17 at 18:13
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The issue with your code is twofold:
The
grep
command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([
and]
). You have the same issue in yoursed
substitution.Filenames outputted by
grep -l
may be mangled. If a file contains an unprintable character (such as newline),grep
will simply drop that from the outputted name. This means thatsed
won't be invoked on that file.
This is assuming you'd like to replace the string within the file content, not in filenames:
To find all files beneath the directory /mnt/d/power/jan/output
that contains the exact string Aux Power [A]
, you would use
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';' -print
To change the string to aux_power
you would extend that command like so:
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';'
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This is assuming GNU sed
.
The grep -Fq
will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F
is used).
The sed
command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).
If you'd like to get the names of the modified files listed in the terminal, then leave the -print
in from the first find
command, after the -exec grep ... ';'
.
Since the sed
expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to
find /mnt/d/power/jan/output -type f
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This applies the substitution to all files under the /mnt/d/power/jan/output
directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).
The issue with your code is twofold:
The
grep
command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([
and]
). You have the same issue in yoursed
substitution.Filenames outputted by
grep -l
may be mangled. If a file contains an unprintable character (such as newline),grep
will simply drop that from the outputted name. This means thatsed
won't be invoked on that file.
This is assuming you'd like to replace the string within the file content, not in filenames:
To find all files beneath the directory /mnt/d/power/jan/output
that contains the exact string Aux Power [A]
, you would use
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';' -print
To change the string to aux_power
you would extend that command like so:
find /mnt/d/power/jan/output -type f
-exec grep -Fq 'Aux Power [A]' ';'
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This is assuming GNU sed
.
The grep -Fq
will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F
is used).
The sed
command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).
If you'd like to get the names of the modified files listed in the terminal, then leave the -print
in from the first find
command, after the -exec grep ... ';'
.
Since the sed
expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to
find /mnt/d/power/jan/output -type f
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'
This applies the substitution to all files under the /mnt/d/power/jan/output
directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).
edited Feb 17 at 14:40
answered Feb 17 at 11:23
Kusalananda
103k13202318
103k13202318
You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
â Gerard H. Pille
Feb 17 at 17:38
@GerardH.Pille Please explain and I will get it right.
â Kusalananda
Feb 17 at 17:50
@GerardH.Pille If you referring to the stringaux_power_w
, then all I can say is that the question asks to replace withaux_power
while his attempt is to replace withaux_power_w
. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
â Kusalananda
Feb 17 at 18:13
add a comment |Â
You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
â Gerard H. Pille
Feb 17 at 17:38
@GerardH.Pille Please explain and I will get it right.
â Kusalananda
Feb 17 at 17:50
@GerardH.Pille If you referring to the stringaux_power_w
, then all I can say is that the question asks to replace withaux_power
while his attempt is to replace withaux_power_w
. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
â Kusalananda
Feb 17 at 18:13
You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
â Gerard H. Pille
Feb 17 at 17:38
You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
â Gerard H. Pille
Feb 17 at 17:38
@GerardH.Pille Please explain and I will get it right.
â Kusalananda
Feb 17 at 17:50
@GerardH.Pille Please explain and I will get it right.
â Kusalananda
Feb 17 at 17:50
@GerardH.Pille If you referring to the string
aux_power_w
, then all I can say is that the question asks to replace with aux_power
while his attempt is to replace with aux_power_w
. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.â Kusalananda
Feb 17 at 18:13
@GerardH.Pille If you referring to the string
aux_power_w
, then all I can say is that the question asks to replace with aux_power
while his attempt is to replace with aux_power_w
. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.â Kusalananda
Feb 17 at 18:13
add a comment |Â
up vote
1
down vote
You should escape the square brackets for grep. "[A]" is the same as "A".
Try
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"
add a comment |Â
up vote
1
down vote
You should escape the square brackets for grep. "[A]" is the same as "A".
Try
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You should escape the square brackets for grep. "[A]" is the same as "A".
Try
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"
You should escape the square brackets for grep. "[A]" is the same as "A".
Try
grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"
answered Feb 17 at 11:00
Gerard H. Pille
1,169212
1,169212
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424761%2freplace-string-with-spaces-in-entire-directory%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Just to clarify: You wish to change file contents, do you?
â Ned64
Feb 17 at 11:11