2

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.

don_crissti
  • 82,805
  • 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. –  Jan 10 '19 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 '19 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 ;-) –  Jan 10 '19 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 '19 at 20:53

1 Answers1

9

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 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'

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"]}'
Stephen Kitt
  • 434,908