What script would I use to count the number of a term and its opposite?

Hello All,

I am looking to write a script to count the number of a term and its opposite and create a new file with said list. I can get the terms to print to the file but only one or the other and not both.
I tried this:

grep -wi done */all.txt | grep -wiv done */all.txt  > "filename"

Hello mcesmcsc,

Good that you have shown us what you have tried to solve your own problem, keep it up.
Could you please do let us know sample of Input_file and expected output in CODE TAGS too it will make question more clearer.

Thanks,
R. Singh

2 Likes

If you really want to count, then consider the -c option for grep.
To concatenate outputs from different commands into one file there are 3 methods:

command1 > outfile
command2 >> outfile

More efficient:

(command1; command2) > outfile

Even more efficient:

{ command1; command2; } > outfile

Can be written as mulit-line:

{
command1
command2
} > outfile
1 Like

With grep and friends you'll get one count per run, so you'll have to go through the file twice. Might not be a problem with small files, but resource consumption increases with sizes. Taylor your solution with a little script like e.g.

awk -v"PAT=done" '$0 ~ PAT {PRES++; next} {ABS++} END {print PRES ORS ABS}' file

or, if you need it case insensitive,

awk -v"PAT=done" 'tolower($0) ~ tolower(PAT) {PRES++; next} {ABS++} END {print PRES ORS ABS}' file
2 Likes

thanks everyone, ill give it try. I guess what im looking for is a list something like this:

"done"
name = value
name = value
name = value
"not done"
name = value
name = value
name = value

--- Post updated at 05:36 PM ---

also im looking into 3 different files and compiling the "term" and opposite "term" to gather the list of a count of both

--- Post updated at 05:45 PM ---

so I used the:

 grep -c done */all.txt  | grep -cv done */all.txt > ProductivityReport.md 

and it only shows the "done" text and not both or list both?

What would you expect? The first grep s the lines with "done" in them and pipes it --- into nowhere! As the second opens the input file again to read from it and discards its stdin. You've been given examples of how to do it correctly.

Your desired output isn't too clear, at least to me.