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.

  • 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

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\

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 file\with\backslash.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'


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"]}'
  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.