Search code examples
bashunixwc

How to redirect an output to wc by pipeline?


count=`cat "Main Dir/$param/sent.txt"|sort -k2|tail -n-$LAST_SENT_EMAILS_TO_CONSIDER|cut -f1 -d" " -d"  "|cut -f2 -d"@"|cut -f1 -d"."|wc -w $domain`

Until the wc, I take "LAST_SENT_EMAILS_TO_CONSIDER" strokes from sent.txt sorted by some parameter that I took.

Now, I want to count how much the content of domain ($domain) repeat in the cut strokes.

I did see similar questions and answers, which say that by reorganizing that command line in other way, it could work. Can somebody help me with this concrete example?


Solution

  • Without knowing the input, I can't test my solution. But if I understand you correctly, you just want to do

    ... | grep -F "$domain" | wc -l
    

    i.e. count how many times $domain appears in the output. -F is used to prevent interpreting of special characters (e.g. . normally matches anything, but if your domain is example.com, you don't want to match exampleXcom).

    BTW, grep can count the line itself, so you can shorten it to

    ... | grep -Fc "$domain"