Let's say that I have a source file named A with a lot of strings of 1 and 2, and whose content is being regurlarly and automatically changed (new strings of 1 and 2 are added), and that I perform a grep on this file to extract '1' strings into another file called B.

grep '1' A > B

Now the next grep operations must not only grep A content into B, but also compare B and A in order to extract new strings (and only new strings) into another file. This must be done repetitively. For that I create a new file C which contains the '1' strings of the updated content of A, compare it with B and extract the difference into a new file named D:

grep '1' A > B
sleep 60
while true; do
grep '1' A > C
grep -v -F -x -f B C > D
sleep 60
done &

The problem is that it won't work properly as B is an old grep of A, so the next time it runs C will only be compared to this old B that is not being updated. Furthermore, D is being overwritten each time.

I need to be able to instruct the script to each time keep a copy of last B, so A is always compared to its previous version, and C always shows the difference between current A and previous, last B. I don't need to keep previous versions of C as I copy them elsewhere.

I figured that I had to use variable file names and I tried to add a $(date) variable in B and C filenames, but obviously it doesn't work as grep doesn't recognize them (as the date changes between each run of the script).

PS: I found a workaround by wiping the content of A between each run, so that only new content is taken into consideration, which eliminates the necessity of using variable names and the second grep operation. I would still like to learn how to call variable file names in bash scripts.

PS2: I am writing on my phone, so don't know how to properly encode the bash script.

  • The question is a bit convoluted for me to follow. However, there is a command called "comm" which will give you lines in A but not in B (comm -23 A B) and you can grep on that output. – Angelo Jan 12 '17 at 19:02

why not simply replace B ?

grep '1' A > B
sleep 60
while true; do
grep '1' A > C
grep -v -F -x -f B C > D
cat C >> B   ####
sleep 60
done &

this way D will only get last 60 sec of 1 in A.

  • @Paka - you should accept this answer – Angelo Jan 12 '17 at 19:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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