4

I had a confusion with this topic between switching between command line and script file interface. I had a nice script written out in the command line that worked, but as soon as I wanted to save it to a .sed file, I remembered I could no longer use the -n. I tried using '!d' flag, but I'm not getting the same output. My question:
Is there a way to put the -n in a .sed file, or some other way to stop the automatic printing when I'm in script file interface? I hate to have to convert from protecting my script from the shell to not protecting it, but I guess there is no way around it?

  • 1
    This question would be much easier to understand with some specifics - are you saying you can't use the -n and -f command line options together for some reason? – steeldriver Jan 26 '17 at 20:59
  • I'm wanting to stop automatic printing when I'm writing a .sed file. I have only used -n on the command line to do this. However, I'm unfamiliar with how to do this in a .sed file because we don't need to protect our sed script from the shell etc... Overall: how do I stop automatic printing when I'm writing a .sed file? – Nack Jan 26 '17 at 21:02
  • 1
    Are you planning to use the file with sed -f from the command line, or using #!/usr/bin/sed -f at the top of the file? Either way, replacing the -f with -nf should work. – Daniel H Jan 26 '17 at 21:26
  • Yes, I plan on using 'sed -f myfile.sed filein' – Nack Jan 26 '17 at 21:50
  • Yes, I need the whole script in the .sed file. Only thing on the command line should be 'sed -f myfile.sed filein'. I want to integrate the turning off the automatic printing in my .sed file, and not on the command line – Nack Jan 26 '17 at 21:54
9

The standard (POSIX) way is to have #n at the start of the script. They have to be the first two bytes of the script.

That precludes the use of a she-bang, that's only to be used for scripts run as sed -f the-script (note that she-bangs are not POSIX and POSIX doesn't specify the path of utilities), but as @Kusalananda said, when using a she-bang, you can always do:

#! /path/to/sed -nf

If you want to make an executable sed script on systems that support she-bangs with arguments (most).

Note that the #n also works on the command line as in:

sed '#n
  s/foo/bar/p'

And you can add more text after like:

#no default output
s/foo/bar/p

Or:

#nifty way to turn -n on
s/foo/bar/p

Actually, that's something to bear in mind to avoid turning -n on by mistake. It's a good idea to use a space after # in comments for that (and for legibility).

# no, I don't want to turn -n on
s/foo/bar/
  • Might be worth pointing out that the #n doesn't replace the #! line for an executable sed script, and you can't use both. Hence a sed script with #n needs to be run with sed -f explicitly on the command line, as far as I can see at least. – Kusalananda Jan 26 '17 at 21:48
2

sed [-e] 'command' and sed -f /path/to/script behave exactly the same way with respect to the -n switch. If you want to execute a sed script and turn off the "print-by-default" behavior, use sed -n -f /path/to/script.sed /path/to/input.

2

You will either have to run the sed script using

$ sed -n -f script.sed

or change the #! ("hash-bang") line in the script to something like

#!/usr/bin/sed -nf

and run it with

$ ./script.sed

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.