Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a linux command wait for the end of input before piping?

I find myself often using linux shell commands, such as grep, sed and awk, to format/filter/forward stuff into something more useable.

The "source" data is usually the output of a process, or the cat of a local file.

As I'm becoming more fluent with the commands, I find myself using them more and more. What I used to do before with a notepad++ regex-search&replace, I now know how to do "on the fly" in my shell.

The "issue" I'm now encountering is how to deal with data I've "copied" from another source, which I want to feed into the shell directly via stdin, and the output I only want to see in stdout. The problem I'm encountering is that there is a "criss-cross" of the input and output. For example:

>awk '$1 == "true" {print $2}'
true Paul
Paul
false John        
true Janice
Janice

The issue is that this is not useable for me. I need the output to come clearly separated from the input. Either it comes afterwards, or I don't want to see stdin at all.

How could we do that? I've found I can use tail to "hold" the input data until it's all been fed:

tail -n 999 | awk '$1 == "true" {print $2}'
true Paul
false John
true Janice
Paul
Janice

This works, but is a little bit cumbersome.

Are there any built-in commands that basically "hold" the stream until it is over (or rather, don't print anything until all input has been processed)? Or ways to simply suppress seeing stdin? I could also use temp files, but I'd like to have a "clean 1 step no overhead" way of doing it...

like image 942
monarch_dodra Avatar asked Feb 12 '23 23:02

monarch_dodra


2 Answers

awk '$1 == "true" {print $2}' | tac | tac

or

awk '$1 == "true" {print $2}' > /tmp/file.txt; cat /tmp/file.txt
like image 142
Vytenis Bivainis Avatar answered Feb 16 '23 01:02

Vytenis Bivainis


I'm not aware of any such command, but you can easily add your own in .bashrc:

waiting() {
  printf "%s\n" "$(cat)"
}

then

awk '$1 == "true" {print $2}' | waiting

You can of course also put waiting first if you prefer.

like image 32
that other guy Avatar answered Feb 16 '23 03:02

that other guy