-2

I need help to subtract 2 of the awk result below. Could someone give me an insight?

  1. Find the total of rows filtered by the specified word in 12th column
  2. Find the total of rows filtered by the specified word in 12th column and has the specified date in column 13
  3. Subtract 1 and 2 and print the result

This solves problem 1

awk -F ',' '$12 ~ /<WORD>/ {count++} END {print count}' file.csv

This solves problem 2

awk -F ',' '$12 ~ /<WORD>/ && $13 ~ /<DATE>/ {count2++} END {print count2}'  file.csv

Unfortunately, I'm not getting the result for problem 3 below.

awk -F ',' '$12 ~ /<WORD>/ {count++} END {print count}' file.csv; awk -F ',' '$12 ~ /<WORD>/ && $13 ~ /<DATE>/ {count2++} END {print count2}'  file.csv; awk {print $count-$count2}
AlphaK12
  • 11
  • 4
  • 3
    This is hard to follow. Some sample input data and expected output would help. It would also help to format your code samples. It isn't clear why you have two different awk scripts. – MichaelR Aug 07 '21 at 05:13
  • The first code sample is solving problem 1 and 2. I’m trying to combine 1 and 2 and subtract them – AlphaK12 Aug 07 '21 at 05:41
  • Great! That is much clearer. Looks like you have an answer too. – MichaelR Aug 07 '21 at 06:00
  • Among other things I'm guessing you're using a partial regexp comparison when you should be using a full string comparison. Its also not clear if the output should contain all 3 values or just the final one after subtraction. If you [edit] your question to include concise, testable sample input and expected output then we can help you. – Ed Morton Aug 07 '21 at 14:43
  • @EdMorton I actually was looking for a partial regexp. I am looking for just the final output (#3). I thought it was basic enough that I didn't need to provide any sample. – AlphaK12 Aug 09 '21 at 02:25

1 Answers1

1

If you run multiple awk commands, the variables used are not shared. If you want them to be shared, you could combine the commands into a single program:

awk -F ',' '
    $12 ~ /<WORD>/ {count++}
   '$12 ~ /<WORD>/ && $13 ~ /<DATE>/ {count2++}
   END {print $count-$count2}
' file.csv

However, your three specifications seem to simplify to:

print the number of the rows of a csv file file.csv which contain a specific word in column 12 and which do not contain a specific date in column 13

awk -F, '$12~/word/ && $13!~/date/ {n++} END {print n+0}' file.csv

where /word/ and /date/ are regular expressions that provide the required word and date respectively.

jhnc
  • 11,310
  • 1
  • 9
  • 26
  • 1
    Thank you so much! I had to add parenthesis between the second regex, and it works great! `awk -F, '$12~/word/ && ! ($13~/date/) {n++} END {print n+0}' file.csv)` – AlphaK12 Aug 09 '21 at 02:21
  • Is it possible to use today's date as the date? This doesn't seem to work `awk -F, '$12~/word/ && ! ($13~/$(date '+%Y%m%d')/) {n++} END {print n+0}' file.csv)` – AlphaK12 Aug 09 '21 at 04:03
  • consider: https://stackoverflow.com/q/6697753/10971581 – jhnc Aug 09 '21 at 04:39