9
echo Hello World | nano - | less

I want to modify Hello -> Goodbye while in the the text editor.

It does not have to be with nano. I am stuck with nano reading stdin but requiring me to write to a file (writing to - creates a file named -)

  • @slm, I think it makes more sense to close the other Q as a dupe of this since this one is the broader case. – Graeme Apr 19 '14 at 20:17
  • @Graeme - I would tend to agree w/ you but the other one has your answer as well, though not as well written. Some merging might be more apt. Maybe we should enlist the help of a mod to help w/ these 2? I'll ping one about it. – slm Apr 19 '14 at 20:24
  • @slm, I think we can leave it to whoever is going through the close queue and let the crowd decide. The main difference as I see it is that the other Q is a special case of this where you only want one end of the pipe. – Graeme Apr 19 '14 at 21:27
  • 1
    @Graeme - if we don't prime the pump on it then ppl will likely close the newer to the older as a dup. – slm Apr 19 '14 at 21:28
  • @slm, we did, I voted to close the other one so both are in the queue and we have this comment thread (provided that people actually read it). – Graeme Apr 19 '14 at 21:36
  • @slm \O/ http://meta.unix.stackexchange.com/questions/2840/which-duplicate-should-be-closed – goldilocks Apr 20 '14 at 09:41
  • @Graeme ^^ (Will watch the repopen queue in case this one gets closed -- maybe that is the only way to get rid of the existing close votes :/) – goldilocks Apr 20 '14 at 09:43
  • @goldilocks Go on... (I was stupid. I didn't save the other question's address thus I cannot close it now. The reopen history seems not to contain this info.) – Hauke Laging May 03 '14 at 08:30

3 Answers3

12

The moreutils package has a great command for doing this, called vipe. From the man page:

SYNOPSIS
       command1 | vipe | command2

DESCRIPTION
       vipe allows you to run your editor in the middle of a unix pipeline and
       edit the data that is being piped between programs. Your editor will have
       the full data being piped from command1 loaded into it, and when you close
       it, that data will be piped into command2.

By default this will use the editor command, which is usually just a symlink to the default command line editor. You can change this by either altering the link (use update-alternatives on Debian based systems) or using the EDITOR environmental variable. Eg, you could do:

echo Hello World | EDITOR=nano vipe | less

Otherwise, if the particular text editor doesn't have support for this kind of thing, I think you are stuck with manually creating a temporary file, writing the file to that, running the editor, inputting the file to the rest of the pipeline and removing the temporary file. The vipe command basically takes care of all this. This is nice, but the command is rarely available by default.

Graeme
  • 34,027
2

If all you want is to replace "Hello" with "Goodbye" (or any other word replacement) you can avoid the manual editing in an editor in favor of automatic search&replace tool. For example "sed" can do such replacements like this:

echo Hello World | sed 's/Hello/Goodbye/' | less

Check "man sed" for more details.

ElazarR
  • 166
1

The text editor joe (aka Joe's Own Editor) does what you want.

The command echo "hello world" | joe - | less functions as expected, although it needs the quotes for some reason.

Some commands (such as gpg) produce displayed output that does not enter the pipe. This corrupts the initial display inside of joe, but hitting ctrl+r -- refresh -- will clean things up.