How to compare two lines in a txt file with bash?Crontab not executing jobs Ubuntu 14.04Parse lines with specific pattern out of fileWhy my bash-script doesn't work with cron?How to delete multiple lines from txt file, based on a variable in previous column?Using source command to replace bash string with field arrayWrite $(date) Into File Using Cron and Bash Shell Script

Being flown out for an interview, is it ok to ask to stay a while longer to check out the area?

Could a society of Kaiju exist logically?

An employer is trying to force me to switch banks, which I know is illegal. What should I do?

ssh login using public keys for users that do not (yet) exist

Should plywood be missing on my roof?

Does anyone know who created or where this He-Man and Battlecat image came from?

Morphing between two functions

In a shuttle launch, what would have happened if all three SSMEs failed during flight?

Monoidal category that is not spacial

How to calculate player health percentage?

Do rainbows show spectral lines from the sun?

Counter intuitive Bayesian theorem

Single word for delaying an unpleasant task

Effects of quantum computing on parallel universes

Hoping for a satisfying conclusion (a musical connect wall)

Open problems from antiquity solved with analytic geometry

Formatting Date and TIME in French

Multiple processes redirecting to the same file with >

Why is this missile positioned in this odd position? How can it be launched correctly?

Stream of Thought/Creeping Chill Interaction?

In Fischer-Petrosian 1971 game 7, why did Fischer give up his good knight for a bad bishop?

Why was Wouter Basson never charged with crimes against humanity for Project Coast?

How to get out of the Ice Palace in Zelda A link to the Past?

Is it unsafe to remove one stud from a load bearing wall?



How to compare two lines in a txt file with bash?


Crontab not executing jobs Ubuntu 14.04Parse lines with specific pattern out of fileWhy my bash-script doesn't work with cron?How to delete multiple lines from txt file, based on a variable in previous column?Using source command to replace bash string with field arrayWrite $(date) Into File Using Cron and Bash Shell Script






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









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 !










share|improve this question




























  • And what should the result be? a boolean "yes they differ" / "no they don't" - or something more complicated?

    – steeldriver
    Jul 15 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 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 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 at 20:39











  • So your goal is to create file B?

    – WinEunuuchs2Unix
    Jul 15 at 21:37

















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 !










share|improve this question




























  • And what should the result be? a boolean "yes they differ" / "no they don't" - or something more complicated?

    – steeldriver
    Jul 15 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 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 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 at 20:39











  • So your goal is to create file B?

    – WinEunuuchs2Unix
    Jul 15 at 21:37













1












1








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 !










share|improve this question

















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 !







bash






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited Jul 15 at 17:41







Mpt700

















asked Jul 15 at 16:57









Mpt700Mpt700

83 bronze badges




83 bronze badges















  • And what should the result be? a boolean "yes they differ" / "no they don't" - or something more complicated?

    – steeldriver
    Jul 15 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 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 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 at 20:39











  • So your goal is to create file B?

    – WinEunuuchs2Unix
    Jul 15 at 21:37

















  • And what should the result be? a boolean "yes they differ" / "no they don't" - or something more complicated?

    – steeldriver
    Jul 15 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 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 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 at 20:39











  • So your goal is to create file B?

    – WinEunuuchs2Unix
    Jul 15 at 21:37
















And what should the result be? a boolean "yes they differ" / "no they don't" - or something more complicated?

– steeldriver
Jul 15 at 16:59





And what should the result be? a boolean "yes they differ" / "no they don't" - or something more complicated?

– steeldriver
Jul 15 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 at 17:26





@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 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 at 19:34





Are you getting a new file every day or a new line added to an existing file every day?

– WinEunuuchs2Unix
Jul 15 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 at 20:39





@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 at 20:39













So your goal is to create file B?

– WinEunuuchs2Unix
Jul 15 at 21:37





So your goal is to create file B?

– WinEunuuchs2Unix
Jul 15 at 21:37










1 Answer
1






active

oldest

votes


















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.






share|improve this answer


























  • Thaank you @WinEunuuchs2Unix . I added more clarity to what I am trying to achieve which could change solution/script a bit .

    – Mpt700
    Jul 15 at 17:35












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1158476%2fhow-to-compare-two-lines-in-a-txt-file-with-bash%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown


























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer


























  • Thaank you @WinEunuuchs2Unix . I added more clarity to what I am trying to achieve which could change solution/script a bit .

    – Mpt700
    Jul 15 at 17:35















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.






share|improve this answer


























  • Thaank you @WinEunuuchs2Unix . I added more clarity to what I am trying to achieve which could change solution/script a bit .

    – Mpt700
    Jul 15 at 17:35













0














0










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.






share|improve this answer














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.







share|improve this answer













share|improve this answer




share|improve this answer










answered Jul 15 at 17:15









WinEunuuchs2UnixWinEunuuchs2Unix

60.4k18 gold badges122 silver badges236 bronze badges




60.4k18 gold badges122 silver badges236 bronze badges















  • Thaank you @WinEunuuchs2Unix . I added more clarity to what I am trying to achieve which could change solution/script a bit .

    – Mpt700
    Jul 15 at 17:35

















  • Thaank you @WinEunuuchs2Unix . I added more clarity to what I am trying to achieve which could change solution/script a bit .

    – Mpt700
    Jul 15 at 17:35
















Thaank you @WinEunuuchs2Unix . I added more clarity to what I am trying to achieve which could change solution/script a bit .

– Mpt700
Jul 15 at 17:35





Thaank you @WinEunuuchs2Unix . I added more clarity to what I am trying to achieve which could change solution/script a bit .

– Mpt700
Jul 15 at 17:35


















draft saved

draft discarded















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1158476%2fhow-to-compare-two-lines-in-a-txt-file-with-bash%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown









Popular posts from this blog

Tamil (spriik) Luke uk diar | Nawigatjuun

Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?