Replace block of text with sed, awk, perl or vim
I am trying to replace every instance of the following block of six rows (meaning that those characters are repeated in many places in the same columns of a big text file, called a.txt)
xx\
xx\
xx\
xx\
xx\
xx\
with this block of six rows, just reducing the six rows block of two columns to a six rows block of only one column
x
x
x
x
x
x
I have tried the following code (seen on unix.stackexchange 1) without success:
sed -i '/xx/,/xx/,/xx/,/xx/,/xx/,/xx/c
x
x
x
x
x
x' a.txt
I have had no better results with awk or vim or perl.
awk sed vim perl
New contributor
add a comment |
I am trying to replace every instance of the following block of six rows (meaning that those characters are repeated in many places in the same columns of a big text file, called a.txt)
xx\
xx\
xx\
xx\
xx\
xx\
with this block of six rows, just reducing the six rows block of two columns to a six rows block of only one column
x
x
x
x
x
x
I have tried the following code (seen on unix.stackexchange 1) without success:
sed -i '/xx/,/xx/,/xx/,/xx/,/xx/,/xx/c
x
x
x
x
x
x' a.txt
I have had no better results with awk or vim or perl.
awk sed vim perl
New contributor
2
You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?
– Kusalananda
6 hours ago
2
Is that lines consisting literally of the two lettersx
and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literallyxx<backslash><backslash>
, can you add an example of what the data actually would be?
– ilkkachu
6 hours ago
add a comment |
I am trying to replace every instance of the following block of six rows (meaning that those characters are repeated in many places in the same columns of a big text file, called a.txt)
xx\
xx\
xx\
xx\
xx\
xx\
with this block of six rows, just reducing the six rows block of two columns to a six rows block of only one column
x
x
x
x
x
x
I have tried the following code (seen on unix.stackexchange 1) without success:
sed -i '/xx/,/xx/,/xx/,/xx/,/xx/,/xx/c
x
x
x
x
x
x' a.txt
I have had no better results with awk or vim or perl.
awk sed vim perl
New contributor
I am trying to replace every instance of the following block of six rows (meaning that those characters are repeated in many places in the same columns of a big text file, called a.txt)
xx\
xx\
xx\
xx\
xx\
xx\
with this block of six rows, just reducing the six rows block of two columns to a six rows block of only one column
x
x
x
x
x
x
I have tried the following code (seen on unix.stackexchange 1) without success:
sed -i '/xx/,/xx/,/xx/,/xx/,/xx/,/xx/c
x
x
x
x
x
x' a.txt
I have had no better results with awk or vim or perl.
awk sed vim perl
awk sed vim perl
New contributor
New contributor
edited 5 hours ago
Rui F Ribeiro
41.4k1481140
41.4k1481140
New contributor
asked 7 hours ago
ocapocap
1
1
New contributor
New contributor
2
You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?
– Kusalananda
6 hours ago
2
Is that lines consisting literally of the two lettersx
and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literallyxx<backslash><backslash>
, can you add an example of what the data actually would be?
– ilkkachu
6 hours ago
add a comment |
2
You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?
– Kusalananda
6 hours ago
2
Is that lines consisting literally of the two lettersx
and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literallyxx<backslash><backslash>
, can you add an example of what the data actually would be?
– ilkkachu
6 hours ago
2
2
You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?
– Kusalananda
6 hours ago
You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?
– Kusalananda
6 hours ago
2
2
Is that lines consisting literally of the two letters
x
and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literally xx<backslash><backslash>
, can you add an example of what the data actually would be?– ilkkachu
6 hours ago
Is that lines consisting literally of the two letters
x
and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literally xx<backslash><backslash>
, can you add an example of what the data actually would be?– ilkkachu
6 hours ago
add a comment |
2 Answers
2
active
oldest
votes
I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\
(two letters ex, two backslashes), replaced with x
:
perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file
^xx\\n
is simply a start of a line, xx
, two escaped backslashes, and a newline. {6}
demands the text in parenthesis to repeat six times. In the replacement part we have x
, backslash, a newline, and that string is "multiplied" by six. (/m
makes ^
match start of line too, /e
makes the replacement a Perl expression instead of just a string, and /g
makes a global replacement, replacing all occurrences.)
add a comment |
You can set your block as the record separator, and then for each line chomp
it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).
perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file
Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.
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
});
}
});
ocap is a new contributor. Be nice, and check out our Code of Conduct.
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%2f505511%2freplace-block-of-text-with-sed-awk-perl-or-vim%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\
(two letters ex, two backslashes), replaced with x
:
perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file
^xx\\n
is simply a start of a line, xx
, two escaped backslashes, and a newline. {6}
demands the text in parenthesis to repeat six times. In the replacement part we have x
, backslash, a newline, and that string is "multiplied" by six. (/m
makes ^
match start of line too, /e
makes the replacement a Perl expression instead of just a string, and /g
makes a global replacement, replacing all occurrences.)
add a comment |
I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\
(two letters ex, two backslashes), replaced with x
:
perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file
^xx\\n
is simply a start of a line, xx
, two escaped backslashes, and a newline. {6}
demands the text in parenthesis to repeat six times. In the replacement part we have x
, backslash, a newline, and that string is "multiplied" by six. (/m
makes ^
match start of line too, /e
makes the replacement a Perl expression instead of just a string, and /g
makes a global replacement, replacing all occurrences.)
add a comment |
I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\
(two letters ex, two backslashes), replaced with x
:
perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file
^xx\\n
is simply a start of a line, xx
, two escaped backslashes, and a newline. {6}
demands the text in parenthesis to repeat six times. In the replacement part we have x
, backslash, a newline, and that string is "multiplied" by six. (/m
makes ^
match start of line too, /e
makes the replacement a Perl expression instead of just a string, and /g
makes a global replacement, replacing all occurrences.)
I would just go with Perl, and slurp the file in to do a single regex substitution on it. With the blocks consisting of the literal string xx\
(two letters ex, two backslashes), replaced with x
:
perl -0777 -pe 's/(^xx\\n){6}/"x\n" x 6/meg' < file
^xx\\n
is simply a start of a line, xx
, two escaped backslashes, and a newline. {6}
demands the text in parenthesis to repeat six times. In the replacement part we have x
, backslash, a newline, and that string is "multiplied" by six. (/m
makes ^
match start of line too, /e
makes the replacement a Perl expression instead of just a string, and /g
makes a global replacement, replacing all occurrences.)
answered 6 hours ago
ilkkachuilkkachu
60.7k1098172
60.7k1098172
add a comment |
add a comment |
You can set your block as the record separator, and then for each line chomp
it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).
perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file
Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.
add a comment |
You can set your block as the record separator, and then for each line chomp
it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).
perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file
Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.
add a comment |
You can set your block as the record separator, and then for each line chomp
it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).
perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file
Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.
You can set your block as the record separator, and then for each line chomp
it and append the replacement block. Notice that the file may not end with such a block (just as it may not end with a newline).
perl -pe 'BEGIN{$/ = "xx\\n" x 6; $r = "x\n" x 6} $_ .= $r if chomp' file
Of course, you can just slurp the whole file, and with small files that's the fastest way out, but that's terrible as an example, because it precludes demonstrating scalable approaches and methods that could be used with any file, no matter how big.
edited 4 hours ago
answered 4 hours ago
Uncle BillyUncle Billy
6476
6476
add a comment |
add a comment |
ocap is a new contributor. Be nice, and check out our Code of Conduct.
ocap is a new contributor. Be nice, and check out our Code of Conduct.
ocap is a new contributor. Be nice, and check out our Code of Conduct.
ocap is a new contributor. Be nice, and check out our Code of Conduct.
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%2f505511%2freplace-block-of-text-with-sed-awk-perl-or-vim%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
2
You mention in your first sentence that the file may have several columns, and that what you show is just one column out of many. Is that so?
– Kusalananda
6 hours ago
2
Is that lines consisting literally of the two letters
x
and two backslashes? I'm not sure how that is two columns, can you edit the question to clarify? And if it's not literallyxx<backslash><backslash>
, can you add an example of what the data actually would be?– ilkkachu
6 hours ago