Replacing string in all files found by grep. Can't get it to work
I find all needed files with grep: grep --include=*.{php,ini,conf,sh} -ril -P "'([dw-_.]+)(@domain.com)'" '/var/www_data/somepath/'
Now I assume it's either use of sed or perl to for replacement process, alas I can't figure out how to use above regexp in either of them.
I saw this way and this way but as I said, I couldn't get it work with my pattern (and by that I mean that I get > in the next line after executing command. I assume it's problem with regexp shown above when used with sed or perl), so any advice on that matter would be nice.
Also, I don't know if it's possible (it's not really important) but I'd like to print some string for each file in with replacement occurred, for example File fixed: /path/to/file/
(file names taken from grep list maybe?)
sed grep regular-expression perl
add a comment |
I find all needed files with grep: grep --include=*.{php,ini,conf,sh} -ril -P "'([dw-_.]+)(@domain.com)'" '/var/www_data/somepath/'
Now I assume it's either use of sed or perl to for replacement process, alas I can't figure out how to use above regexp in either of them.
I saw this way and this way but as I said, I couldn't get it work with my pattern (and by that I mean that I get > in the next line after executing command. I assume it's problem with regexp shown above when used with sed or perl), so any advice on that matter would be nice.
Also, I don't know if it's possible (it's not really important) but I'd like to print some string for each file in with replacement occurred, for example File fixed: /path/to/file/
(file names taken from grep list maybe?)
sed grep regular-expression perl
Can you explain what you are trying to do? (P.S. yessed
andperl
can be used to search and replace. Notgrep
.)
– ctrl-alt-delor
Sep 6 '14 at 21:12
Yeah, I know grep is not for replacements, just for search. I'm just trying to replace all email addresses under certain domain which are listed in double single quotes in huge amount of files.
– Igor Yavych
Sep 6 '14 at 21:59
Also see unix.stackexchange.com/q/32666. It usesgrep
andxargs
to touch only files with a match.
– jww
Nov 11 '16 at 20:03
add a comment |
I find all needed files with grep: grep --include=*.{php,ini,conf,sh} -ril -P "'([dw-_.]+)(@domain.com)'" '/var/www_data/somepath/'
Now I assume it's either use of sed or perl to for replacement process, alas I can't figure out how to use above regexp in either of them.
I saw this way and this way but as I said, I couldn't get it work with my pattern (and by that I mean that I get > in the next line after executing command. I assume it's problem with regexp shown above when used with sed or perl), so any advice on that matter would be nice.
Also, I don't know if it's possible (it's not really important) but I'd like to print some string for each file in with replacement occurred, for example File fixed: /path/to/file/
(file names taken from grep list maybe?)
sed grep regular-expression perl
I find all needed files with grep: grep --include=*.{php,ini,conf,sh} -ril -P "'([dw-_.]+)(@domain.com)'" '/var/www_data/somepath/'
Now I assume it's either use of sed or perl to for replacement process, alas I can't figure out how to use above regexp in either of them.
I saw this way and this way but as I said, I couldn't get it work with my pattern (and by that I mean that I get > in the next line after executing command. I assume it's problem with regexp shown above when used with sed or perl), so any advice on that matter would be nice.
Also, I don't know if it's possible (it's not really important) but I'd like to print some string for each file in with replacement occurred, for example File fixed: /path/to/file/
(file names taken from grep list maybe?)
sed grep regular-expression perl
sed grep regular-expression perl
edited Feb 15 at 5:26
Rui F Ribeiro
40.7k1479137
40.7k1479137
asked Sep 6 '14 at 21:03
Igor YavychIgor Yavych
226139
226139
Can you explain what you are trying to do? (P.S. yessed
andperl
can be used to search and replace. Notgrep
.)
– ctrl-alt-delor
Sep 6 '14 at 21:12
Yeah, I know grep is not for replacements, just for search. I'm just trying to replace all email addresses under certain domain which are listed in double single quotes in huge amount of files.
– Igor Yavych
Sep 6 '14 at 21:59
Also see unix.stackexchange.com/q/32666. It usesgrep
andxargs
to touch only files with a match.
– jww
Nov 11 '16 at 20:03
add a comment |
Can you explain what you are trying to do? (P.S. yessed
andperl
can be used to search and replace. Notgrep
.)
– ctrl-alt-delor
Sep 6 '14 at 21:12
Yeah, I know grep is not for replacements, just for search. I'm just trying to replace all email addresses under certain domain which are listed in double single quotes in huge amount of files.
– Igor Yavych
Sep 6 '14 at 21:59
Also see unix.stackexchange.com/q/32666. It usesgrep
andxargs
to touch only files with a match.
– jww
Nov 11 '16 at 20:03
Can you explain what you are trying to do? (P.S. yes
sed
and perl
can be used to search and replace. Not grep
.)– ctrl-alt-delor
Sep 6 '14 at 21:12
Can you explain what you are trying to do? (P.S. yes
sed
and perl
can be used to search and replace. Not grep
.)– ctrl-alt-delor
Sep 6 '14 at 21:12
Yeah, I know grep is not for replacements, just for search. I'm just trying to replace all email addresses under certain domain which are listed in double single quotes in huge amount of files.
– Igor Yavych
Sep 6 '14 at 21:59
Yeah, I know grep is not for replacements, just for search. I'm just trying to replace all email addresses under certain domain which are listed in double single quotes in huge amount of files.
– Igor Yavych
Sep 6 '14 at 21:59
Also see unix.stackexchange.com/q/32666. It uses
grep
and xargs
to touch only files with a match.– jww
Nov 11 '16 at 20:03
Also see unix.stackexchange.com/q/32666. It uses
grep
and xargs
to touch only files with a match.– jww
Nov 11 '16 at 20:03
add a comment |
3 Answers
3
active
oldest
votes
Typically, when you get a >
in the next line after hitting, it means that one of your quotes isn't closed yet. I couldn't find that mistake in your regex. But you do not need to surround the path /var/www_data/somepath/
with single quotes. I assume there are no unusual characters in somepath
?
Anyways, I tested your regex with sed. dw
look like vim
syntax for me, that's why I translated it to ascii (which always works). Also, inside of you do not need to escape
.
:
sed -r "s/'([A-Za-z0-9_-.]+)(@domain.com)'/'adsf'/g" test.dat
Indeed you can use sed
or perl
for your task. You don't necessarily need grep
to generate a file list, unless you have GB of data. Then presorting could result in a speed benefit.
To test your regex, you could do the following:
cd /var/www_data/somepath/
sed -r 's|pattern|replace-pattern|g' a_single_file.php
When you're satisfied with the result, just add the -ibak
(--in-place=bak
) argument and run it on all files
find . -type f -name '*.php' -o -name '*.ini' -o name '*.conf' -o -name '*.sh'
-exec sed -r -ibak 's|pattern|replace-pattern|g' '{}' ;
The original files are being put into <orignalname.php>.bak
.
To answer your last question. For this job, grep
is the tool you want, you could run it on the .bak
files generated by sed above:
grep --recursive --include='*.bak' -E --files-with-matches 'pattern' . > files_fixed.txt
or, simply:
find . -type f -name '*.bak'
Thefind
piped intosed
messes with all the file times, and not just the files where the match occurs.
– jww
Nov 11 '16 at 20:00
add a comment |
With GNU sed
you can use sed -i
.
sed -i 'script' *.{php,ini,conf,sh}
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
add a comment |
Most Linux:
sed -i 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
On MacOS:
sed -i '' 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
The sed in MacOS is expecting a backup parameter after -i, use empty string if you don't need backup files. The "g" is for global replace, otherwise it's only the first per row.
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f154136%2freplacing-string-in-all-files-found-by-grep-cant-get-it-to-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Typically, when you get a >
in the next line after hitting, it means that one of your quotes isn't closed yet. I couldn't find that mistake in your regex. But you do not need to surround the path /var/www_data/somepath/
with single quotes. I assume there are no unusual characters in somepath
?
Anyways, I tested your regex with sed. dw
look like vim
syntax for me, that's why I translated it to ascii (which always works). Also, inside of you do not need to escape
.
:
sed -r "s/'([A-Za-z0-9_-.]+)(@domain.com)'/'adsf'/g" test.dat
Indeed you can use sed
or perl
for your task. You don't necessarily need grep
to generate a file list, unless you have GB of data. Then presorting could result in a speed benefit.
To test your regex, you could do the following:
cd /var/www_data/somepath/
sed -r 's|pattern|replace-pattern|g' a_single_file.php
When you're satisfied with the result, just add the -ibak
(--in-place=bak
) argument and run it on all files
find . -type f -name '*.php' -o -name '*.ini' -o name '*.conf' -o -name '*.sh'
-exec sed -r -ibak 's|pattern|replace-pattern|g' '{}' ;
The original files are being put into <orignalname.php>.bak
.
To answer your last question. For this job, grep
is the tool you want, you could run it on the .bak
files generated by sed above:
grep --recursive --include='*.bak' -E --files-with-matches 'pattern' . > files_fixed.txt
or, simply:
find . -type f -name '*.bak'
Thefind
piped intosed
messes with all the file times, and not just the files where the match occurs.
– jww
Nov 11 '16 at 20:00
add a comment |
Typically, when you get a >
in the next line after hitting, it means that one of your quotes isn't closed yet. I couldn't find that mistake in your regex. But you do not need to surround the path /var/www_data/somepath/
with single quotes. I assume there are no unusual characters in somepath
?
Anyways, I tested your regex with sed. dw
look like vim
syntax for me, that's why I translated it to ascii (which always works). Also, inside of you do not need to escape
.
:
sed -r "s/'([A-Za-z0-9_-.]+)(@domain.com)'/'adsf'/g" test.dat
Indeed you can use sed
or perl
for your task. You don't necessarily need grep
to generate a file list, unless you have GB of data. Then presorting could result in a speed benefit.
To test your regex, you could do the following:
cd /var/www_data/somepath/
sed -r 's|pattern|replace-pattern|g' a_single_file.php
When you're satisfied with the result, just add the -ibak
(--in-place=bak
) argument and run it on all files
find . -type f -name '*.php' -o -name '*.ini' -o name '*.conf' -o -name '*.sh'
-exec sed -r -ibak 's|pattern|replace-pattern|g' '{}' ;
The original files are being put into <orignalname.php>.bak
.
To answer your last question. For this job, grep
is the tool you want, you could run it on the .bak
files generated by sed above:
grep --recursive --include='*.bak' -E --files-with-matches 'pattern' . > files_fixed.txt
or, simply:
find . -type f -name '*.bak'
Thefind
piped intosed
messes with all the file times, and not just the files where the match occurs.
– jww
Nov 11 '16 at 20:00
add a comment |
Typically, when you get a >
in the next line after hitting, it means that one of your quotes isn't closed yet. I couldn't find that mistake in your regex. But you do not need to surround the path /var/www_data/somepath/
with single quotes. I assume there are no unusual characters in somepath
?
Anyways, I tested your regex with sed. dw
look like vim
syntax for me, that's why I translated it to ascii (which always works). Also, inside of you do not need to escape
.
:
sed -r "s/'([A-Za-z0-9_-.]+)(@domain.com)'/'adsf'/g" test.dat
Indeed you can use sed
or perl
for your task. You don't necessarily need grep
to generate a file list, unless you have GB of data. Then presorting could result in a speed benefit.
To test your regex, you could do the following:
cd /var/www_data/somepath/
sed -r 's|pattern|replace-pattern|g' a_single_file.php
When you're satisfied with the result, just add the -ibak
(--in-place=bak
) argument and run it on all files
find . -type f -name '*.php' -o -name '*.ini' -o name '*.conf' -o -name '*.sh'
-exec sed -r -ibak 's|pattern|replace-pattern|g' '{}' ;
The original files are being put into <orignalname.php>.bak
.
To answer your last question. For this job, grep
is the tool you want, you could run it on the .bak
files generated by sed above:
grep --recursive --include='*.bak' -E --files-with-matches 'pattern' . > files_fixed.txt
or, simply:
find . -type f -name '*.bak'
Typically, when you get a >
in the next line after hitting, it means that one of your quotes isn't closed yet. I couldn't find that mistake in your regex. But you do not need to surround the path /var/www_data/somepath/
with single quotes. I assume there are no unusual characters in somepath
?
Anyways, I tested your regex with sed. dw
look like vim
syntax for me, that's why I translated it to ascii (which always works). Also, inside of you do not need to escape
.
:
sed -r "s/'([A-Za-z0-9_-.]+)(@domain.com)'/'adsf'/g" test.dat
Indeed you can use sed
or perl
for your task. You don't necessarily need grep
to generate a file list, unless you have GB of data. Then presorting could result in a speed benefit.
To test your regex, you could do the following:
cd /var/www_data/somepath/
sed -r 's|pattern|replace-pattern|g' a_single_file.php
When you're satisfied with the result, just add the -ibak
(--in-place=bak
) argument and run it on all files
find . -type f -name '*.php' -o -name '*.ini' -o name '*.conf' -o -name '*.sh'
-exec sed -r -ibak 's|pattern|replace-pattern|g' '{}' ;
The original files are being put into <orignalname.php>.bak
.
To answer your last question. For this job, grep
is the tool you want, you could run it on the .bak
files generated by sed above:
grep --recursive --include='*.bak' -E --files-with-matches 'pattern' . > files_fixed.txt
or, simply:
find . -type f -name '*.bak'
edited Sep 6 '14 at 22:44
answered Sep 6 '14 at 21:15
SebastianSebastian
5,39133047
5,39133047
Thefind
piped intosed
messes with all the file times, and not just the files where the match occurs.
– jww
Nov 11 '16 at 20:00
add a comment |
Thefind
piped intosed
messes with all the file times, and not just the files where the match occurs.
– jww
Nov 11 '16 at 20:00
The
find
piped into sed
messes with all the file times, and not just the files where the match occurs.– jww
Nov 11 '16 at 20:00
The
find
piped into sed
messes with all the file times, and not just the files where the match occurs.– jww
Nov 11 '16 at 20:00
add a comment |
With GNU sed
you can use sed -i
.
sed -i 'script' *.{php,ini,conf,sh}
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
add a comment |
With GNU sed
you can use sed -i
.
sed -i 'script' *.{php,ini,conf,sh}
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
add a comment |
With GNU sed
you can use sed -i
.
sed -i 'script' *.{php,ini,conf,sh}
With GNU sed
you can use sed -i
.
sed -i 'script' *.{php,ini,conf,sh}
answered Sep 6 '14 at 21:36
mikeservmikeserv
45.8k668159
45.8k668159
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
add a comment |
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
sed
messes with all the file times, and not just the files where a match would occur from grep
.– jww
Nov 11 '16 at 20:01
sed
messes with all the file times, and not just the files where a match would occur from grep
.– jww
Nov 11 '16 at 20:01
add a comment |
Most Linux:
sed -i 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
On MacOS:
sed -i '' 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
The sed in MacOS is expecting a backup parameter after -i, use empty string if you don't need backup files. The "g" is for global replace, otherwise it's only the first per row.
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
add a comment |
Most Linux:
sed -i 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
On MacOS:
sed -i '' 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
The sed in MacOS is expecting a backup parameter after -i, use empty string if you don't need backup files. The "g" is for global replace, otherwise it's only the first per row.
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
add a comment |
Most Linux:
sed -i 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
On MacOS:
sed -i '' 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
The sed in MacOS is expecting a backup parameter after -i, use empty string if you don't need backup files. The "g" is for global replace, otherwise it's only the first per row.
Most Linux:
sed -i 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
On MacOS:
sed -i '' 's#FIND#REPLACE#g' *.{php,ini,conf,sh}
The sed in MacOS is expecting a backup parameter after -i, use empty string if you don't need backup files. The "g" is for global replace, otherwise it's only the first per row.
answered Sep 6 '14 at 22:42
user83305user83305
111
111
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
add a comment |
sed
messes with all the file times, and not just the files where a match would occur fromgrep
.
– jww
Nov 11 '16 at 20:01
sed
messes with all the file times, and not just the files where a match would occur from grep
.– jww
Nov 11 '16 at 20:01
sed
messes with all the file times, and not just the files where a match would occur from grep
.– jww
Nov 11 '16 at 20:01
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f154136%2freplacing-string-in-all-files-found-by-grep-cant-get-it-to-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you explain what you are trying to do? (P.S. yes
sed
andperl
can be used to search and replace. Notgrep
.)– ctrl-alt-delor
Sep 6 '14 at 21:12
Yeah, I know grep is not for replacements, just for search. I'm just trying to replace all email addresses under certain domain which are listed in double single quotes in huge amount of files.
– Igor Yavych
Sep 6 '14 at 21:59
Also see unix.stackexchange.com/q/32666. It uses
grep
andxargs
to touch only files with a match.– jww
Nov 11 '16 at 20:03