In Linux, would it be possible to create a multi-pipe command, serving the same type of command ie: grep however different output alteration done to it?

Strict example:

grep 10-Feb file.txt | awk '{print $2}' | cut -d . -f 1,2 | sort | uniq -c | sort -rn | head -20 ; 
grep 10-Feb file.txt | awk '{print $2}' | sort | uniq -c | sort -rn | head ; 
grep 10-Feb file-txt [..]

Would it be possible to make the first grep and path an argument, and make it automatically apply to the following commands?

$=[grep 10-Feb file.txt] | awk '{print $2}' | cut -d . -f 1,2 | sort | uniq -c | sort -rn | head -20 ; 
$ | awk '{print $2}' | sort | uniq -c | sort -rn | head ; 
$ [..]

As an edit:

I cannot set-up the above command in a bash script since I have limited access to root from which I can call the scripts from.

I am interested in assigning a command as a variable from the command line solely.

  • You can save the result of the first command, and then reuse it several times. – lgeorget Mar 30 '17 at 13:19

By "in Linux" you probably mean "in Bash".

And the answer is yes, they are called functions:


In ruby (which runs in linux) you can load your code blocks in lambdas (which are unnamed functions)

  • Hey Bruno! I am actually interested in doing in non-bash situations. Essentially interested in running a single command using variables. In the current case, due to limitations, I cannot use a bash script, solely the commands. – George Mar 30 '17 at 13:57
  • You know, this answer be a lot more useful if it didn't rely on only the link but e.g. gave an example relevant to this situation. Answers with only links also have the risk that the link will rot, making the answer pretty much useless. – ilkkachu Mar 30 '17 at 14:12
  • @ilkkachu You are generally right, but I suspect that tldp.org will outlive the SO stack (it is official documentation, not some blog). I also think that OP need to be pointed to some quality bash documentation. – Bruno9779 Mar 30 '17 at 14:22
  • @Bruno9779, I was under the impression that as a GNU program, Bash's official documentation would be maintained by the GNU project. In any case, a document from 2000 might not be the best reference, and this answer does not contain the actual answer, just a pointer. – ilkkachu Mar 30 '17 at 14:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.