Can sed produce output in the form of text file(s) all by itself as long as the input came from STDIN?












1















What the title says: assuming sed reads from STDIN1 could its output be a text file ?

Just to be clear, I'm not talking about saving sed's output to a text file via shell operators like >, >> or using tools like tee etc.

The question here is whether sed alone can produce output in the form of text file(s) and if so, under which circumstances ?





1: this is simply to rule out any in-place editing of a text file with implementations that support it.










share|improve this question

























  • If the question is about being able to redirect sed's output from within sed (the accepted reply suggests that), then please change the title and the text of your question to reflect it; the argument to the w command of sed doesn' t have to be a "text file"; it could be a character device, pipe, socket, etc.

    – pizdelect
    Jan 10 at 14:14











  • @pizdelect - there's nothing to change, really: the question is whether sed alone can produce a text file and if so how / when does that happen. If you still don't understand it then feel free to down-vote it. If you already did so, then enjoy your vote.

    – don_crissti
    Jan 10 at 19:07






  • 1





    Changing the question to something more relevant and precise would help people looking for info -- for instance, even the text from your comment "whether sed alone can produce (create?) a (text) file ..." is MUCH better than the one from the question ;-)

    – pizdelect
    Jan 10 at 20:49













  • @pizdelect - if you take the time to read the question it says exactly that but whatever, I'll edit it... is it better now ?

    – don_crissti
    Jan 10 at 20:53


















1















What the title says: assuming sed reads from STDIN1 could its output be a text file ?

Just to be clear, I'm not talking about saving sed's output to a text file via shell operators like >, >> or using tools like tee etc.

The question here is whether sed alone can produce output in the form of text file(s) and if so, under which circumstances ?





1: this is simply to rule out any in-place editing of a text file with implementations that support it.










share|improve this question

























  • If the question is about being able to redirect sed's output from within sed (the accepted reply suggests that), then please change the title and the text of your question to reflect it; the argument to the w command of sed doesn' t have to be a "text file"; it could be a character device, pipe, socket, etc.

    – pizdelect
    Jan 10 at 14:14











  • @pizdelect - there's nothing to change, really: the question is whether sed alone can produce a text file and if so how / when does that happen. If you still don't understand it then feel free to down-vote it. If you already did so, then enjoy your vote.

    – don_crissti
    Jan 10 at 19:07






  • 1





    Changing the question to something more relevant and precise would help people looking for info -- for instance, even the text from your comment "whether sed alone can produce (create?) a (text) file ..." is MUCH better than the one from the question ;-)

    – pizdelect
    Jan 10 at 20:49













  • @pizdelect - if you take the time to read the question it says exactly that but whatever, I'll edit it... is it better now ?

    – don_crissti
    Jan 10 at 20:53
















1












1








1








What the title says: assuming sed reads from STDIN1 could its output be a text file ?

Just to be clear, I'm not talking about saving sed's output to a text file via shell operators like >, >> or using tools like tee etc.

The question here is whether sed alone can produce output in the form of text file(s) and if so, under which circumstances ?





1: this is simply to rule out any in-place editing of a text file with implementations that support it.










share|improve this question
















What the title says: assuming sed reads from STDIN1 could its output be a text file ?

Just to be clear, I'm not talking about saving sed's output to a text file via shell operators like >, >> or using tools like tee etc.

The question here is whether sed alone can produce output in the form of text file(s) and if so, under which circumstances ?





1: this is simply to rule out any in-place editing of a text file with implementations that support it.







sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 10 at 21:05







don_crissti

















asked Jan 10 at 12:21









don_crisstidon_crissti

51.3k15137165




51.3k15137165













  • If the question is about being able to redirect sed's output from within sed (the accepted reply suggests that), then please change the title and the text of your question to reflect it; the argument to the w command of sed doesn' t have to be a "text file"; it could be a character device, pipe, socket, etc.

    – pizdelect
    Jan 10 at 14:14











  • @pizdelect - there's nothing to change, really: the question is whether sed alone can produce a text file and if so how / when does that happen. If you still don't understand it then feel free to down-vote it. If you already did so, then enjoy your vote.

    – don_crissti
    Jan 10 at 19:07






  • 1





    Changing the question to something more relevant and precise would help people looking for info -- for instance, even the text from your comment "whether sed alone can produce (create?) a (text) file ..." is MUCH better than the one from the question ;-)

    – pizdelect
    Jan 10 at 20:49













  • @pizdelect - if you take the time to read the question it says exactly that but whatever, I'll edit it... is it better now ?

    – don_crissti
    Jan 10 at 20:53





















  • If the question is about being able to redirect sed's output from within sed (the accepted reply suggests that), then please change the title and the text of your question to reflect it; the argument to the w command of sed doesn' t have to be a "text file"; it could be a character device, pipe, socket, etc.

    – pizdelect
    Jan 10 at 14:14











  • @pizdelect - there's nothing to change, really: the question is whether sed alone can produce a text file and if so how / when does that happen. If you still don't understand it then feel free to down-vote it. If you already did so, then enjoy your vote.

    – don_crissti
    Jan 10 at 19:07






  • 1





    Changing the question to something more relevant and precise would help people looking for info -- for instance, even the text from your comment "whether sed alone can produce (create?) a (text) file ..." is MUCH better than the one from the question ;-)

    – pizdelect
    Jan 10 at 20:49













  • @pizdelect - if you take the time to read the question it says exactly that but whatever, I'll edit it... is it better now ?

    – don_crissti
    Jan 10 at 20:53



















If the question is about being able to redirect sed's output from within sed (the accepted reply suggests that), then please change the title and the text of your question to reflect it; the argument to the w command of sed doesn' t have to be a "text file"; it could be a character device, pipe, socket, etc.

– pizdelect
Jan 10 at 14:14





If the question is about being able to redirect sed's output from within sed (the accepted reply suggests that), then please change the title and the text of your question to reflect it; the argument to the w command of sed doesn' t have to be a "text file"; it could be a character device, pipe, socket, etc.

– pizdelect
Jan 10 at 14:14













@pizdelect - there's nothing to change, really: the question is whether sed alone can produce a text file and if so how / when does that happen. If you still don't understand it then feel free to down-vote it. If you already did so, then enjoy your vote.

– don_crissti
Jan 10 at 19:07





@pizdelect - there's nothing to change, really: the question is whether sed alone can produce a text file and if so how / when does that happen. If you still don't understand it then feel free to down-vote it. If you already did so, then enjoy your vote.

– don_crissti
Jan 10 at 19:07




1




1





Changing the question to something more relevant and precise would help people looking for info -- for instance, even the text from your comment "whether sed alone can produce (create?) a (text) file ..." is MUCH better than the one from the question ;-)

– pizdelect
Jan 10 at 20:49







Changing the question to something more relevant and precise would help people looking for info -- for instance, even the text from your comment "whether sed alone can produce (create?) a (text) file ..." is MUCH better than the one from the question ;-)

– pizdelect
Jan 10 at 20:49















@pizdelect - if you take the time to read the question it says exactly that but whatever, I'll edit it... is it better now ?

– don_crissti
Jan 10 at 20:53







@pizdelect - if you take the time to read the question it says exactly that but whatever, I'll edit it... is it better now ?

– don_crissti
Jan 10 at 20:53












1 Answer
1






active

oldest

votes


















8














Yes, using the w command, or the w flag to the s command, both of which are specified by POSIX.



For example, with



sed -n 'w output.txt'


anything fed into sed’s standard input will be written to output.txt, with no output to sed’s standard output (thanks to the -n option). This can be combined with other commands to manipulate the pattern space before writing it out.



A few notes of warning and recommendations:



If the file name starts with a blank, use a ./ prefix:



sed -n 'w ./ output with blanks.txt'


If the file name contains newline characters, POSIXly, you can escape them with backslash, though it won't work with all sed implementations (in particular, not with GNU sed nor busybox sed):



sed -n 'w ./output
with
newline.txt'


If it contains backslash characters, in some implementations (the POSIX ones above that support newlines in file names), you need to escape them with backslash:



sed -n 'w file\with\backslash.txt' # POSIX
sed -n 'w filewithbackslash.txt' # GNU


Also note that the output file will be created (or truncated) even if the input is empty or the w command is never run like in:



sed 'd;w file'


Or



seq 20 | sed '/21/ w file'


You may want to use awk instead to avoid this kind of problems



FILE=arbitrary-file-name awk '/100/ {print > ENVIRON["FILE"]}'





share|improve this answer


























  • also the w flag of the s/// command. A standard sed can use a file name in the r and w commands or as an argument to the w flag of s///. This has security implication, because you cannot pledge the sed command as being just a filter.

    – pizdelect
    Jan 10 at 12:35






  • 2





    See also the W command of GNU sed.

    – Stéphane Chazelas
    Jan 10 at 13:06











  • Thanks for confirming my statement ! ;)

    – don_crissti
    Jan 10 at 19:37













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493697%2fcan-sed-produce-output-in-the-form-of-text-files-all-by-itself-as-long-as-the%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














Yes, using the w command, or the w flag to the s command, both of which are specified by POSIX.



For example, with



sed -n 'w output.txt'


anything fed into sed’s standard input will be written to output.txt, with no output to sed’s standard output (thanks to the -n option). This can be combined with other commands to manipulate the pattern space before writing it out.



A few notes of warning and recommendations:



If the file name starts with a blank, use a ./ prefix:



sed -n 'w ./ output with blanks.txt'


If the file name contains newline characters, POSIXly, you can escape them with backslash, though it won't work with all sed implementations (in particular, not with GNU sed nor busybox sed):



sed -n 'w ./output
with
newline.txt'


If it contains backslash characters, in some implementations (the POSIX ones above that support newlines in file names), you need to escape them with backslash:



sed -n 'w file\with\backslash.txt' # POSIX
sed -n 'w filewithbackslash.txt' # GNU


Also note that the output file will be created (or truncated) even if the input is empty or the w command is never run like in:



sed 'd;w file'


Or



seq 20 | sed '/21/ w file'


You may want to use awk instead to avoid this kind of problems



FILE=arbitrary-file-name awk '/100/ {print > ENVIRON["FILE"]}'





share|improve this answer


























  • also the w flag of the s/// command. A standard sed can use a file name in the r and w commands or as an argument to the w flag of s///. This has security implication, because you cannot pledge the sed command as being just a filter.

    – pizdelect
    Jan 10 at 12:35






  • 2





    See also the W command of GNU sed.

    – Stéphane Chazelas
    Jan 10 at 13:06











  • Thanks for confirming my statement ! ;)

    – don_crissti
    Jan 10 at 19:37


















8














Yes, using the w command, or the w flag to the s command, both of which are specified by POSIX.



For example, with



sed -n 'w output.txt'


anything fed into sed’s standard input will be written to output.txt, with no output to sed’s standard output (thanks to the -n option). This can be combined with other commands to manipulate the pattern space before writing it out.



A few notes of warning and recommendations:



If the file name starts with a blank, use a ./ prefix:



sed -n 'w ./ output with blanks.txt'


If the file name contains newline characters, POSIXly, you can escape them with backslash, though it won't work with all sed implementations (in particular, not with GNU sed nor busybox sed):



sed -n 'w ./output
with
newline.txt'


If it contains backslash characters, in some implementations (the POSIX ones above that support newlines in file names), you need to escape them with backslash:



sed -n 'w file\with\backslash.txt' # POSIX
sed -n 'w filewithbackslash.txt' # GNU


Also note that the output file will be created (or truncated) even if the input is empty or the w command is never run like in:



sed 'd;w file'


Or



seq 20 | sed '/21/ w file'


You may want to use awk instead to avoid this kind of problems



FILE=arbitrary-file-name awk '/100/ {print > ENVIRON["FILE"]}'





share|improve this answer


























  • also the w flag of the s/// command. A standard sed can use a file name in the r and w commands or as an argument to the w flag of s///. This has security implication, because you cannot pledge the sed command as being just a filter.

    – pizdelect
    Jan 10 at 12:35






  • 2





    See also the W command of GNU sed.

    – Stéphane Chazelas
    Jan 10 at 13:06











  • Thanks for confirming my statement ! ;)

    – don_crissti
    Jan 10 at 19:37
















8












8








8







Yes, using the w command, or the w flag to the s command, both of which are specified by POSIX.



For example, with



sed -n 'w output.txt'


anything fed into sed’s standard input will be written to output.txt, with no output to sed’s standard output (thanks to the -n option). This can be combined with other commands to manipulate the pattern space before writing it out.



A few notes of warning and recommendations:



If the file name starts with a blank, use a ./ prefix:



sed -n 'w ./ output with blanks.txt'


If the file name contains newline characters, POSIXly, you can escape them with backslash, though it won't work with all sed implementations (in particular, not with GNU sed nor busybox sed):



sed -n 'w ./output
with
newline.txt'


If it contains backslash characters, in some implementations (the POSIX ones above that support newlines in file names), you need to escape them with backslash:



sed -n 'w file\with\backslash.txt' # POSIX
sed -n 'w filewithbackslash.txt' # GNU


Also note that the output file will be created (or truncated) even if the input is empty or the w command is never run like in:



sed 'd;w file'


Or



seq 20 | sed '/21/ w file'


You may want to use awk instead to avoid this kind of problems



FILE=arbitrary-file-name awk '/100/ {print > ENVIRON["FILE"]}'





share|improve this answer















Yes, using the w command, or the w flag to the s command, both of which are specified by POSIX.



For example, with



sed -n 'w output.txt'


anything fed into sed’s standard input will be written to output.txt, with no output to sed’s standard output (thanks to the -n option). This can be combined with other commands to manipulate the pattern space before writing it out.



A few notes of warning and recommendations:



If the file name starts with a blank, use a ./ prefix:



sed -n 'w ./ output with blanks.txt'


If the file name contains newline characters, POSIXly, you can escape them with backslash, though it won't work with all sed implementations (in particular, not with GNU sed nor busybox sed):



sed -n 'w ./output
with
newline.txt'


If it contains backslash characters, in some implementations (the POSIX ones above that support newlines in file names), you need to escape them with backslash:



sed -n 'w file\with\backslash.txt' # POSIX
sed -n 'w filewithbackslash.txt' # GNU


Also note that the output file will be created (or truncated) even if the input is empty or the w command is never run like in:



sed 'd;w file'


Or



seq 20 | sed '/21/ w file'


You may want to use awk instead to avoid this kind of problems



FILE=arbitrary-file-name awk '/100/ {print > ENVIRON["FILE"]}'






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 10 at 13:02









Stéphane Chazelas

308k57581939




308k57581939










answered Jan 10 at 12:27









Stephen KittStephen Kitt

174k24398473




174k24398473













  • also the w flag of the s/// command. A standard sed can use a file name in the r and w commands or as an argument to the w flag of s///. This has security implication, because you cannot pledge the sed command as being just a filter.

    – pizdelect
    Jan 10 at 12:35






  • 2





    See also the W command of GNU sed.

    – Stéphane Chazelas
    Jan 10 at 13:06











  • Thanks for confirming my statement ! ;)

    – don_crissti
    Jan 10 at 19:37





















  • also the w flag of the s/// command. A standard sed can use a file name in the r and w commands or as an argument to the w flag of s///. This has security implication, because you cannot pledge the sed command as being just a filter.

    – pizdelect
    Jan 10 at 12:35






  • 2





    See also the W command of GNU sed.

    – Stéphane Chazelas
    Jan 10 at 13:06











  • Thanks for confirming my statement ! ;)

    – don_crissti
    Jan 10 at 19:37



















also the w flag of the s/// command. A standard sed can use a file name in the r and w commands or as an argument to the w flag of s///. This has security implication, because you cannot pledge the sed command as being just a filter.

– pizdelect
Jan 10 at 12:35





also the w flag of the s/// command. A standard sed can use a file name in the r and w commands or as an argument to the w flag of s///. This has security implication, because you cannot pledge the sed command as being just a filter.

– pizdelect
Jan 10 at 12:35




2




2





See also the W command of GNU sed.

– Stéphane Chazelas
Jan 10 at 13:06





See also the W command of GNU sed.

– Stéphane Chazelas
Jan 10 at 13:06













Thanks for confirming my statement ! ;)

– don_crissti
Jan 10 at 19:37







Thanks for confirming my statement ! ;)

– don_crissti
Jan 10 at 19:37




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493697%2fcan-sed-produce-output-in-the-form-of-text-files-all-by-itself-as-long-as-the%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

張江高科駅