Search code examples
ex

How to pipe the output of `program` directly to `ex` without saving it as a temp file?


I have a program which print to stdout.

$ ./program > log.txt
$ cat log.txt
[A]
00: efbb bf52 454d 2047 454e 5245 204f 7065
10: 7261 0d0a 5245 4d20 4441 5445 2032 3031
20: 310d 0a52 454d 2044 4953 4349 4420 3235

[B]
00: 3132 4334 3134 0d0a 5245 4d20 434f 4d4d
10: 454e 5420 2245 7861 6374 4175 6469 6f43
20: 6f70 7920 7631 2e30 6231 220d 0a50 4552

[C]
00: 464f 524d 4552 2022 5661 7269 6f75 7322
10: 0d0a 5449 544c 4520 2220 5468 6520 4f70
20: 6572 6120 4344 2031 220d 0a46 494c 4520

I want to pipe program's output to xxd to generate more information.

$ echo -e 'g/^00/,/^20/!xxd -r | xxd\n%p' | ex file.log
[A]
0000000: efbb bf52 454d 2047 454e 5245 204f 7065  ...REM GENRE Ope
0000010: 7261 0d0a 5245 4d20 4441 5445 2032 3031  ra..REM DATE 201
0000020: 310d 0a52 454d 2044 4953 4349 4420 3235  1..REM DISCID 25

[B]
0000000: 3132 4334 3134 0d0a 5245 4d20 434f 4d4d  12C414..REM COMM
0000010: 454e 5420 2245 7861 6374 4175 6469 6f43  ENT "ExactAudioC
0000020: 6f70 7920 7631 2e30 6231 220d 0a50 4552  opy v1.0b1"..PER

[C]
0000000: 464f 524d 4552 2022 5661 7269 6f75 7322  FORMER "Various"
0000010: 0d0a 5449 544c 4520 2220 5468 6520 4f70  ..TITLE " The Op
0000020: 6572 6120 4344 2031 220d 0a46 494c 4520  era CD 1"..FILE

Here is the problem:
How to pipe the output of program directly to ex without saving it as a temp file?


note: prefer to using standard tools(such as, ed, ex, sed, awk, etc.)


Solution

  • ex accepts "-" as the input file. This indicates ex to read from stdin. You can probably do something like:

    ./program | ex -c "your command1" -c "your command2" -

    If -c can not be used to your intent, then maybe you can use redirection to introduce them manually as ex man page says that when stdin is redirected it uses stderr as input for commands.