How can I replace a newline with its escape sequence?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Using sed
to make text that contains certain characters suitable for use in string literals is straightforward:
sed "s/\\/\\\\/g"
sed "s/\"/\\\"/g"
But how can I do something similar with a text file containing newline characters?
sed awk
add a comment |
Using sed
to make text that contains certain characters suitable for use in string literals is straightforward:
sed "s/\\/\\\\/g"
sed "s/\"/\\\"/g"
But how can I do something similar with a text file containing newline characters?
sed awk
sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.
– barlop
Jan 18 '18 at 4:08
add a comment |
Using sed
to make text that contains certain characters suitable for use in string literals is straightforward:
sed "s/\\/\\\\/g"
sed "s/\"/\\\"/g"
But how can I do something similar with a text file containing newline characters?
sed awk
Using sed
to make text that contains certain characters suitable for use in string literals is straightforward:
sed "s/\\/\\\\/g"
sed "s/\"/\\\"/g"
But how can I do something similar with a text file containing newline characters?
sed awk
sed awk
asked Aug 12 '15 at 13:34
MelabMelab
34711021
34711021
sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.
– barlop
Jan 18 '18 at 4:08
add a comment |
sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.
– barlop
Jan 18 '18 at 4:08
sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.
– barlop
Jan 18 '18 at 4:08
sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.
– barlop
Jan 18 '18 at 4:08
add a comment |
4 Answers
4
active
oldest
votes
Use:
sed ':a;N;$!ba;s/n/\n/g'
Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.
add a comment |
sed is line based, and this can cause issues when trying to replace newline characters.
the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.
I would suggest that 'tr' would probably be a better fit here.
as an example, to replace newline characters with spaces:
tr 'n' ' ' < inputfile
Won't[rn]
be cleaner?
– theoden
Aug 12 '15 at 14:03
I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.
– David Moylan
Aug 12 '15 at 14:10
I tried usingtr "n" "\n"
before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.
– Melab
Aug 12 '15 at 20:12
@Melab correct. the 'tr' command will only do a single character replacement.
– David Moylan
Aug 15 '15 at 0:04
So then how does it help at all?
– Melab
Aug 17 '15 at 0:41
|
show 1 more comment
I'd like to extend David Moytan's solution:
cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'
add a comment |
I had this problem too.
You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.
echo -n $(sed 'a
\n');
Stick whatever you're sed-ing in the parentheses before the sed command.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f955935%2fhow-can-i-replace-a-newline-with-its-escape-sequence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use:
sed ':a;N;$!ba;s/n/\n/g'
Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.
add a comment |
Use:
sed ':a;N;$!ba;s/n/\n/g'
Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.
add a comment |
Use:
sed ':a;N;$!ba;s/n/\n/g'
Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.
Use:
sed ':a;N;$!ba;s/n/\n/g'
Which uses the answer from How can I replace a newline (n) using sed? substituting an escaped newline character as the replacement to match the question here.
edited Feb 7 at 20:59
Mikey T.K.
2,18231941
2,18231941
answered Feb 19 '18 at 3:19
vossad01vossad01
1314
1314
add a comment |
add a comment |
sed is line based, and this can cause issues when trying to replace newline characters.
the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.
I would suggest that 'tr' would probably be a better fit here.
as an example, to replace newline characters with spaces:
tr 'n' ' ' < inputfile
Won't[rn]
be cleaner?
– theoden
Aug 12 '15 at 14:03
I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.
– David Moylan
Aug 12 '15 at 14:10
I tried usingtr "n" "\n"
before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.
– Melab
Aug 12 '15 at 20:12
@Melab correct. the 'tr' command will only do a single character replacement.
– David Moylan
Aug 15 '15 at 0:04
So then how does it help at all?
– Melab
Aug 17 '15 at 0:41
|
show 1 more comment
sed is line based, and this can cause issues when trying to replace newline characters.
the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.
I would suggest that 'tr' would probably be a better fit here.
as an example, to replace newline characters with spaces:
tr 'n' ' ' < inputfile
Won't[rn]
be cleaner?
– theoden
Aug 12 '15 at 14:03
I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.
– David Moylan
Aug 12 '15 at 14:10
I tried usingtr "n" "\n"
before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.
– Melab
Aug 12 '15 at 20:12
@Melab correct. the 'tr' command will only do a single character replacement.
– David Moylan
Aug 15 '15 at 0:04
So then how does it help at all?
– Melab
Aug 17 '15 at 0:41
|
show 1 more comment
sed is line based, and this can cause issues when trying to replace newline characters.
the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.
I would suggest that 'tr' would probably be a better fit here.
as an example, to replace newline characters with spaces:
tr 'n' ' ' < inputfile
sed is line based, and this can cause issues when trying to replace newline characters.
the official documentation for sed makes a specific reference to newline characters and states they are stripped off before being passed to sed.
I would suggest that 'tr' would probably be a better fit here.
as an example, to replace newline characters with spaces:
tr 'n' ' ' < inputfile
answered Aug 12 '15 at 14:01
David MoylanDavid Moylan
973
973
Won't[rn]
be cleaner?
– theoden
Aug 12 '15 at 14:03
I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.
– David Moylan
Aug 12 '15 at 14:10
I tried usingtr "n" "\n"
before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.
– Melab
Aug 12 '15 at 20:12
@Melab correct. the 'tr' command will only do a single character replacement.
– David Moylan
Aug 15 '15 at 0:04
So then how does it help at all?
– Melab
Aug 17 '15 at 0:41
|
show 1 more comment
Won't[rn]
be cleaner?
– theoden
Aug 12 '15 at 14:03
I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.
– David Moylan
Aug 12 '15 at 14:10
I tried usingtr "n" "\n"
before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.
– Melab
Aug 12 '15 at 20:12
@Melab correct. the 'tr' command will only do a single character replacement.
– David Moylan
Aug 15 '15 at 0:04
So then how does it help at all?
– Melab
Aug 17 '15 at 0:41
Won't
[rn]
be cleaner?– theoden
Aug 12 '15 at 14:03
Won't
[rn]
be cleaner?– theoden
Aug 12 '15 at 14:03
I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.
– David Moylan
Aug 12 '15 at 14:10
I guess that all depends on the source data and requirements. OP only referred to newline. I would recommend to try both and see which produces the best result. YMMV.
– David Moylan
Aug 12 '15 at 14:10
I tried using
tr "n" "\n"
before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.– Melab
Aug 12 '15 at 20:12
I tried using
tr "n" "\n"
before I posted this question and it just replaced the newline characters with backslashes. It seems to only turn a sequence into one character only.– Melab
Aug 12 '15 at 20:12
@Melab correct. the 'tr' command will only do a single character replacement.
– David Moylan
Aug 15 '15 at 0:04
@Melab correct. the 'tr' command will only do a single character replacement.
– David Moylan
Aug 15 '15 at 0:04
So then how does it help at all?
– Melab
Aug 17 '15 at 0:41
So then how does it help at all?
– Melab
Aug 17 '15 at 0:41
|
show 1 more comment
I'd like to extend David Moytan's solution:
cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'
add a comment |
I'd like to extend David Moytan's solution:
cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'
add a comment |
I'd like to extend David Moytan's solution:
cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'
I'd like to extend David Moytan's solution:
cat /etc/passwd | perl -e 'while(<>) { $_ =~ s/[rn]/__NEWLINE__/g; print "$_" }'
edited Mar 20 '17 at 10:17
Community♦
1
1
answered Aug 12 '15 at 14:11
theodentheoden
544519
544519
add a comment |
add a comment |
I had this problem too.
You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.
echo -n $(sed 'a
\n');
Stick whatever you're sed-ing in the parentheses before the sed command.
add a comment |
I had this problem too.
You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.
echo -n $(sed 'a
\n');
Stick whatever you're sed-ing in the parentheses before the sed command.
add a comment |
I had this problem too.
You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.
echo -n $(sed 'a
\n');
Stick whatever you're sed-ing in the parentheses before the sed command.
I had this problem too.
You can use the a function to start every newline with the proper escape sequence, then pipe the resulting function into echo -n which suppresses newlines.
echo -n $(sed 'a
\n');
Stick whatever you're sed-ing in the parentheses before the sed command.
edited Jan 18 '18 at 1:31
RockPaperLizard
3,231133672
3,231133672
answered Jan 18 '18 at 1:10
Kaeden WileKaeden Wile
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f955935%2fhow-can-i-replace-a-newline-with-its-escape-sequence%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
sed won't show you the new line character in the find portion, sed is old too, like a legacy program. You could use perl, it has equivalents to sed's find and replace.
– barlop
Jan 18 '18 at 4:08