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;
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
|
show 3 more comments
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
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
|
show 3 more comments
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
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
bash
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
|
show 3 more comments
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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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.
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
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment
|
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.
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
add a comment
|
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.
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.
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
add a comment
|
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
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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