1

I would like to compare the two last lines from the txt file. While it looks like comparing line by line is out there - found few examples - I cant seem to find the way of comparing always the last two lines. And both of those lines will always contain simple set of characters so its like comparing strings. Thank you!

EDIT: What I'm actually trying to achieve might make this question irrelevant so let me explain. I have a script which produces some string value. Now, this script is being run by crontab on daily basis and its output is saved to a new file. Whenever there's a change to the string the file should be updated with it and new file with a new value should be created which records the date of when the new value was created . Since as far as I know crontab jobs are not conditional, I just can't figure it out how I could achieve this goal with not adding a string to the same file and comparing it with a previous line's string . I know that's not the best way since the file will keep growing - but I can always run some clean up (automate it) - however I don't know the other way. And of course the check of those 2 lines would be done by a 2nd script which would be run by 2nd crontab job which would run after the daily 1st job .. I hope the explanation makes sense. Thank you !

8
  • And what should the result be? a boolean "yes they differ" / "no they don't" - or something more complicated? – steeldriver Jul 15 '19 at 16:59
  • @steeldriver it would have to print the last line (should it differ from the 2nd last value) to a new file with a date when was the 2nd value introduced. – Mpt700 Jul 15 '19 at 17:26
  • Are you getting a new file every day or a new line added to an existing file every day? – WinEunuuchs2Unix Jul 15 '19 at 19:34
  • @WinEunuuchs2Unix I have one file (A) where I add 1 line every day. Now, I do have another file (B) - it gets created IF the value which is added to the file A differs from the previous one . In file B I throw the date when the different value has been added to file A . I hope that makes sense. – Mpt700 Jul 15 '19 at 20:39
  • So your goal is to create file B? – WinEunuuchs2Unix Jul 15 '19 at 21:37
0

In psuedo code:

LastLine=""
SecondLastLine=""
while read LineIn
    SecondLastLine="$LastLine"
    LastLine="$LineIn"
done
# now string compare last lines

Typed on phone but I can post non psuedo code answer after work.

1
  • Thaank you @WinEunuuchs2Unix . I added more clarity to what I am trying to achieve which could change solution/script a bit . – Mpt700 Jul 15 '19 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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