1

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.

1
  • You can save the result of the first command, and then reuse it several times. Commented Mar 30, 2017 at 13:19

1 Answer 1

0

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

And the answer is yes, they are called functions:

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

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

4
  • 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. Commented Mar 30, 2017 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. Commented Mar 30, 2017 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. Commented Mar 30, 2017 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. Commented Mar 30, 2017 at 14:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.