1

Let a.out be the program which produces lots of messages to stdout, e.g.:

$ ./a.out

A: abc
B: xyz
A: aaa
A: bbb
B: zzz
C: fff
...

What I need is to distinct the output into separate files, one should contain lines with A: prefix, second with B: and so all, leaving the lines which does not match any pattern to stdout.

Of course one can redirect the whole output of a.out, and then greping it. But if output file will use much disk space, there may be not enough space for grep result.

It would be nice to have some partial-redirect command which can be used in that case like that:

./a.out | partial-redirect ^A: a.txt | partial-redirect ^B: b.txt | partial-redirect ^C: c.txt > rest.txt

Is such command exist and common, or you should implement something yourself to solve this task?

2 Answers 2

3
awk -F':' '$1 ~ "[AB]" { print > $1 } $1 !~ "[AB]" { print > "otheroutput" } ' input

Or, for a general case that will split the entire input based on the first field:

awk -F':' '{ print > $1 }' input

In use:

$ cat input
A: abc
B: xyz
A: aaa
A: bbb
B: zzz
C: fff
$ awk -F':' '$1 ~ "[AB]" { print > $1.txt } $1 !~ "[AB]" { print > "otheroutput" } ' input
$ cat A
A: abc
A: aaa
A: bbb
$ cat B
B: xyz
B: zzz
$ cat otheroutput
C: fff
3
  • It seems to fit only when the distinction criteria is line startings, correct me if I'm wrong. For example, if you want to redirect lines containing logic error, network error, etc., that approach won't work?
    – Anton K
    Jul 17, 2017 at 18:04
  • I went based on the provided example. This approach would be adaptable to anything you can detect in awk.
    – DopeGhoti
    Jul 17, 2017 at 18:06
  • Ok, so it seems that there is no tool for that and you have to implement something yourself. I don't think this example can be considered well-known scenario of awk usage.
    – Anton K
    Jul 19, 2017 at 10:25
1

That's what tee is for in bash:

./a.out | tee >(grep A >fileA) | tee >(grep B >fileB) | grep C >fileC

or even simpler in zsh:

./a.out > >(grep A >fileA) > >(grep B >fileB) > >(grep C >fileC)
1
  • tee writes the whole stdin to stdout, I need to leave only lines that do not match pattern, so the second grep should be faster.
    – Anton K
    Jul 17, 2017 at 18:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .