1

When a Linux command gets multiple files as input. Is there any command/trick to split command's stdout into sections. One for each file? So, it becomes visually clear which lines belong to which file?

For example, when commands such as cat, grep, sed, sort, tail, get multiple files as input. Generally, they print their results continuously on stdout, But I want to split the result into sections. One for each of the input files

I do NOT want to split the result into files, or split it by the number of lines as it is asked in this question. The closest thing that I found to it is using find -print -exec command as it is explained here. But still not quite what I wanted.

Assuming that my command works on file1, file2, and file3, as input, and prints the following:

line1 of file1
line2 of file1
line3 of file1
line1 of file2
line2 of file2
line3 of file2
line1 of file3
line2 of file3
line3 of file3

I want the output to be formatted something similar to the following, so when I observed what I was looking for in a file, I can jump to the next file:

>>> file1 <<<
line1 of file1
line2 of file1
line3 of file1

----------------
>>> file2 <<<
line1 of file2
line2 of file2
line3 of file2

----------------
>>> file3 <<<
line1 of file3
line2 of file3
line3 of file3

Thanks in advance.

Dagelf
  • 266
Sam
  • 11
  • 2
    Make a loop: for f in "file1 file2 file"; do echo ">>> $f <<<"; your_command $f; done – dirkt Apr 14 '20 at 04:11
  • 1
    Does this help? https://serverfault.com/questions/172284/linux-cat-with-separators-among-files – Eduardo Trápani Apr 14 '20 at 04:17
  • Are you asking how you put banners between parts of the output? – ctrl-alt-delor Apr 14 '20 at 07:22
  • @ctrl-alt-delor Yes. Banner, as well as the header. Or anything that visually divides the results of different files, just like the way that "head" command does. – Sam Apr 14 '20 at 14:48
  • I updated this title for you. But i recommend that you edit the question. Work of the grammar and clarity. I got near to the end, when I suddenly realised what you were asking. It was like a complete about turn. You led me one way, then bang. You told me that it was about something else. You are not sending them to different places. – ctrl-alt-delor Apr 14 '20 at 15:26
  • @ctrl-alt-delor Thanks for the tip. I tried to make it more clear. But, keep in mind that English is not my native language. – Sam Apr 14 '20 at 22:45
  • You English writing is good. Better than many natives. If we think clearly, and write at our own level, then everything is good. I could not even guess that you are non-native English writer. – ctrl-alt-delor Apr 15 '20 at 09:52

1 Answers1

0

If you don't care about the ordering of the lines other than having them sorted by file, you can do this with sort, by prepending each line with the id eg.

file1: line1
file2: line1
file1: line2

Then you can pipe the result through sort:

$ ( process-file1 & process-file2 & ); wait | sort
file1: line1
file1: line2
file2: line1

You can of course also add line numbers with this approach - then simply use "sort -n".

To process it into the exact format you want, you can construct a script with awk to strip the front part and turn it into a heading.

Dagelf
  • 266