Use GREP to replace a body of text
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
0
down vote
favorite
What is best way to use GREP to replace text from in
through </div>
withwalked down the path
The cat in the hat
jdlsknklnvlsdknld ======> The cat walked down the path
jkgsabkjkjksjakh
</div>
command-line text-processing grep
add a comment |Â
up vote
0
down vote
favorite
What is best way to use GREP to replace text from in
through </div>
withwalked down the path
The cat in the hat
jdlsknklnvlsdknld ======> The cat walked down the path
jkgsabkjkjksjakh
</div>
command-line text-processing grep
grep doesn't modify text.
â muru
Jan 28 at 6:16
You can use the command 'sed' for that.
â NickT
Jan 28 at 7:27
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
What is best way to use GREP to replace text from in
through </div>
withwalked down the path
The cat in the hat
jdlsknklnvlsdknld ======> The cat walked down the path
jkgsabkjkjksjakh
</div>
command-line text-processing grep
What is best way to use GREP to replace text from in
through </div>
withwalked down the path
The cat in the hat
jdlsknklnvlsdknld ======> The cat walked down the path
jkgsabkjkjksjakh
</div>
command-line text-processing grep
command-line text-processing grep
edited Jan 28 at 4:44
muru
131k19275473
131k19275473
asked Jan 28 at 4:08
David L. Rodgers
1
1
grep doesn't modify text.
â muru
Jan 28 at 6:16
You can use the command 'sed' for that.
â NickT
Jan 28 at 7:27
add a comment |Â
grep doesn't modify text.
â muru
Jan 28 at 6:16
You can use the command 'sed' for that.
â NickT
Jan 28 at 7:27
grep doesn't modify text.
â muru
Jan 28 at 6:16
grep doesn't modify text.
â muru
Jan 28 at 6:16
You can use the command 'sed' for that.
â NickT
Jan 28 at 7:27
You can use the command 'sed' for that.
â NickT
Jan 28 at 7:27
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
Matching multiple lines can be fairly easily done using Perl.
perl -p0e 's# in .*</div># walked down the path.#sm'
Briefly put,
-p0e
turn Perl into ased
-like streaming multi-line processor.- The
s#...#...#
portion signifies that you want to replace all characters fromin
to</div>
(the first clause) withwalked down the path
(the second clause). sm
roughly means "treat newline symbols as all other characters.
This answer assumes that the input text has only one such pattern.
Relevant documentation pages can be found here and here on Perl's official website.
add a comment |Â
up vote
2
down vote
Use grep
to replace text? Good luck with that.
sed
will do it for you:
sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file
Notes
s/old/new/
replaceold
withnew
;
separates commands/begin/,/end/
operate within a range from the line with/begin/
to the line with/end/
grouping, for commands to use on the range
/The cat/n
exclude the line with/The cat/
(do nothing and read the next line)d
delete (the rest of the lines in the range)
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Matching multiple lines can be fairly easily done using Perl.
perl -p0e 's# in .*</div># walked down the path.#sm'
Briefly put,
-p0e
turn Perl into ased
-like streaming multi-line processor.- The
s#...#...#
portion signifies that you want to replace all characters fromin
to</div>
(the first clause) withwalked down the path
(the second clause). sm
roughly means "treat newline symbols as all other characters.
This answer assumes that the input text has only one such pattern.
Relevant documentation pages can be found here and here on Perl's official website.
add a comment |Â
up vote
3
down vote
Matching multiple lines can be fairly easily done using Perl.
perl -p0e 's# in .*</div># walked down the path.#sm'
Briefly put,
-p0e
turn Perl into ased
-like streaming multi-line processor.- The
s#...#...#
portion signifies that you want to replace all characters fromin
to</div>
(the first clause) withwalked down the path
(the second clause). sm
roughly means "treat newline symbols as all other characters.
This answer assumes that the input text has only one such pattern.
Relevant documentation pages can be found here and here on Perl's official website.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Matching multiple lines can be fairly easily done using Perl.
perl -p0e 's# in .*</div># walked down the path.#sm'
Briefly put,
-p0e
turn Perl into ased
-like streaming multi-line processor.- The
s#...#...#
portion signifies that you want to replace all characters fromin
to</div>
(the first clause) withwalked down the path
(the second clause). sm
roughly means "treat newline symbols as all other characters.
This answer assumes that the input text has only one such pattern.
Relevant documentation pages can be found here and here on Perl's official website.
Matching multiple lines can be fairly easily done using Perl.
perl -p0e 's# in .*</div># walked down the path.#sm'
Briefly put,
-p0e
turn Perl into ased
-like streaming multi-line processor.- The
s#...#...#
portion signifies that you want to replace all characters fromin
to</div>
(the first clause) withwalked down the path
(the second clause). sm
roughly means "treat newline symbols as all other characters.
This answer assumes that the input text has only one such pattern.
Relevant documentation pages can be found here and here on Perl's official website.
edited Jan 28 at 16:01
answered Jan 28 at 15:55
undercat
1209
1209
add a comment |Â
add a comment |Â
up vote
2
down vote
Use grep
to replace text? Good luck with that.
sed
will do it for you:
sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file
Notes
s/old/new/
replaceold
withnew
;
separates commands/begin/,/end/
operate within a range from the line with/begin/
to the line with/end/
grouping, for commands to use on the range
/The cat/n
exclude the line with/The cat/
(do nothing and read the next line)d
delete (the rest of the lines in the range)
add a comment |Â
up vote
2
down vote
Use grep
to replace text? Good luck with that.
sed
will do it for you:
sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file
Notes
s/old/new/
replaceold
withnew
;
separates commands/begin/,/end/
operate within a range from the line with/begin/
to the line with/end/
grouping, for commands to use on the range
/The cat/n
exclude the line with/The cat/
(do nothing and read the next line)d
delete (the rest of the lines in the range)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Use grep
to replace text? Good luck with that.
sed
will do it for you:
sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file
Notes
s/old/new/
replaceold
withnew
;
separates commands/begin/,/end/
operate within a range from the line with/begin/
to the line with/end/
grouping, for commands to use on the range
/The cat/n
exclude the line with/The cat/
(do nothing and read the next line)d
delete (the rest of the lines in the range)
Use grep
to replace text? Good luck with that.
sed
will do it for you:
sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file
Notes
s/old/new/
replaceold
withnew
;
separates commands/begin/,/end/
operate within a range from the line with/begin/
to the line with/end/
grouping, for commands to use on the range
/The cat/n
exclude the line with/The cat/
(do nothing and read the next line)d
delete (the rest of the lines in the range)
answered Jan 28 at 7:41
![](https://i.stack.imgur.com/8CW8e.png?s=32&g=1)
![](https://i.stack.imgur.com/8CW8e.png?s=32&g=1)
Zanna
48.3k13120229
48.3k13120229
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2faskubuntu.com%2fquestions%2f1000576%2fuse-grep-to-replace-a-body-of-text%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
grep doesn't modify text.
â muru
Jan 28 at 6:16
You can use the command 'sed' for that.
â NickT
Jan 28 at 7:27