On Error print previous actual and next record












0















In the following example, in column 2. values are increasing, Then the purpose is to get records which are not in increase order and print ( previous - error - next )



file



2343 290232732   
2343 290232738
2349 290202757
2342 290232758
2343 290232813
2341 291063213
2343 291043555
2345 291063609
2343 291063622
2343 291063637
2341 291053637
2343 291063702
2343 291063707
2343 291063712
2343 291063751


I tried



awk '{                                                                                                                               
if(substr($0,6,9) < prev -1)
{printf ("** ERROR ** %4d --> %4d --> %4dn",prv2, prev, substr($0,6,9)) } ; prv2 = prev; prev = substr($0,6,9)}' file


How I can add ; exit 1 to the code in order to stop the process after the last error are found.



I got



** ERROR ** 290232732 --> 290232738 --> 290202757
** ERROR ** 290232813 --> 291063213 --> 291043555
** ERROR ** 291063622 --> 291063637 --> 291053637


Desired Output



** ERROR ** 290232738 --> 290202757 --> 290232758
** ERROR ** 291063213 --> 291043555 --> 291063609
** ERROR ** 291063637 --> 291053637 --> 291063702


Thanks in advance










share|improve this question























  • I don't think there is any way to know when is the last error without going through the whole file at least once.

    – jas
    Nov 13 '18 at 17:48











  • jas, tks, in that case will be enough to get the errors as desired

    – OXXO
    Nov 13 '18 at 19:39
















0















In the following example, in column 2. values are increasing, Then the purpose is to get records which are not in increase order and print ( previous - error - next )



file



2343 290232732   
2343 290232738
2349 290202757
2342 290232758
2343 290232813
2341 291063213
2343 291043555
2345 291063609
2343 291063622
2343 291063637
2341 291053637
2343 291063702
2343 291063707
2343 291063712
2343 291063751


I tried



awk '{                                                                                                                               
if(substr($0,6,9) < prev -1)
{printf ("** ERROR ** %4d --> %4d --> %4dn",prv2, prev, substr($0,6,9)) } ; prv2 = prev; prev = substr($0,6,9)}' file


How I can add ; exit 1 to the code in order to stop the process after the last error are found.



I got



** ERROR ** 290232732 --> 290232738 --> 290202757
** ERROR ** 290232813 --> 291063213 --> 291043555
** ERROR ** 291063622 --> 291063637 --> 291053637


Desired Output



** ERROR ** 290232738 --> 290202757 --> 290232758
** ERROR ** 291063213 --> 291043555 --> 291063609
** ERROR ** 291063637 --> 291053637 --> 291063702


Thanks in advance










share|improve this question























  • I don't think there is any way to know when is the last error without going through the whole file at least once.

    – jas
    Nov 13 '18 at 17:48











  • jas, tks, in that case will be enough to get the errors as desired

    – OXXO
    Nov 13 '18 at 19:39














0












0








0








In the following example, in column 2. values are increasing, Then the purpose is to get records which are not in increase order and print ( previous - error - next )



file



2343 290232732   
2343 290232738
2349 290202757
2342 290232758
2343 290232813
2341 291063213
2343 291043555
2345 291063609
2343 291063622
2343 291063637
2341 291053637
2343 291063702
2343 291063707
2343 291063712
2343 291063751


I tried



awk '{                                                                                                                               
if(substr($0,6,9) < prev -1)
{printf ("** ERROR ** %4d --> %4d --> %4dn",prv2, prev, substr($0,6,9)) } ; prv2 = prev; prev = substr($0,6,9)}' file


How I can add ; exit 1 to the code in order to stop the process after the last error are found.



I got



** ERROR ** 290232732 --> 290232738 --> 290202757
** ERROR ** 290232813 --> 291063213 --> 291043555
** ERROR ** 291063622 --> 291063637 --> 291053637


Desired Output



** ERROR ** 290232738 --> 290202757 --> 290232758
** ERROR ** 291063213 --> 291043555 --> 291063609
** ERROR ** 291063637 --> 291053637 --> 291063702


Thanks in advance










share|improve this question














In the following example, in column 2. values are increasing, Then the purpose is to get records which are not in increase order and print ( previous - error - next )



file



2343 290232732   
2343 290232738
2349 290202757
2342 290232758
2343 290232813
2341 291063213
2343 291043555
2345 291063609
2343 291063622
2343 291063637
2341 291053637
2343 291063702
2343 291063707
2343 291063712
2343 291063751


I tried



awk '{                                                                                                                               
if(substr($0,6,9) < prev -1)
{printf ("** ERROR ** %4d --> %4d --> %4dn",prv2, prev, substr($0,6,9)) } ; prv2 = prev; prev = substr($0,6,9)}' file


How I can add ; exit 1 to the code in order to stop the process after the last error are found.



I got



** ERROR ** 290232732 --> 290232738 --> 290202757
** ERROR ** 290232813 --> 291063213 --> 291043555
** ERROR ** 291063622 --> 291063637 --> 291053637


Desired Output



** ERROR ** 290232738 --> 290202757 --> 290232758
** ERROR ** 291063213 --> 291043555 --> 291063609
** ERROR ** 291063637 --> 291053637 --> 291063702


Thanks in advance







awk






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 15:22









OXXOOXXO

33929




33929













  • I don't think there is any way to know when is the last error without going through the whole file at least once.

    – jas
    Nov 13 '18 at 17:48











  • jas, tks, in that case will be enough to get the errors as desired

    – OXXO
    Nov 13 '18 at 19:39



















  • I don't think there is any way to know when is the last error without going through the whole file at least once.

    – jas
    Nov 13 '18 at 17:48











  • jas, tks, in that case will be enough to get the errors as desired

    – OXXO
    Nov 13 '18 at 19:39

















I don't think there is any way to know when is the last error without going through the whole file at least once.

– jas
Nov 13 '18 at 17:48





I don't think there is any way to know when is the last error without going through the whole file at least once.

– jas
Nov 13 '18 at 17:48













jas, tks, in that case will be enough to get the errors as desired

– OXXO
Nov 13 '18 at 19:39





jas, tks, in that case will be enough to get the errors as desired

– OXXO
Nov 13 '18 at 19:39












1 Answer
1






active

oldest

votes


















1














You can try this awk :



awk '
f { # if flag print ERROR
p = p FS "-->" FS $2
print "** ERROR **" FS p
split ( p , a ) # keep $2 of previous line in p
p = a[3] # this way if it is in error you can catch it
f = 0 # remove flag
}
$2 > p { # $2 is ok keep it
p = $2
next
}
{
p = p FS "-->" FS $2 # $2 is not ok
f = 1 # prepare to print
}
END {
if ( f ) # the last line is in error
print "** ERROR **" FS p # print it
}
' file





share|improve this answer
























  • hi ctac, the code works, but always prints the last line of file? also it considers as error if the previous value is the same as the actual..?.. Kindly can you fix it. Tks

    – OXXO
    Nov 13 '18 at 19:22











  • If the previous value is the same as the actual, it's not in increase order. You can change the line $2 > p by $2 >= p. 'always prints the last line of file?' sorry, I don't understand. $2 of last line is printed only if last line is in error or previous last line is in error.

    – ctac_
    Nov 13 '18 at 19:52











  • Hi catc, tks for the code and explanation, sorry for 'always prints the last line of file' in my file i had 2 extra empty lines at the end of file, that was the reason the code prints it.. But it is Ok now. Tks a lot

    – OXXO
    Nov 14 '18 at 5:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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/3.0/"u003ecc by-sa 3.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%2fstackoverflow.com%2fquestions%2f53284188%2fon-error-print-previous-actual-and-next-record%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









1














You can try this awk :



awk '
f { # if flag print ERROR
p = p FS "-->" FS $2
print "** ERROR **" FS p
split ( p , a ) # keep $2 of previous line in p
p = a[3] # this way if it is in error you can catch it
f = 0 # remove flag
}
$2 > p { # $2 is ok keep it
p = $2
next
}
{
p = p FS "-->" FS $2 # $2 is not ok
f = 1 # prepare to print
}
END {
if ( f ) # the last line is in error
print "** ERROR **" FS p # print it
}
' file





share|improve this answer
























  • hi ctac, the code works, but always prints the last line of file? also it considers as error if the previous value is the same as the actual..?.. Kindly can you fix it. Tks

    – OXXO
    Nov 13 '18 at 19:22











  • If the previous value is the same as the actual, it's not in increase order. You can change the line $2 > p by $2 >= p. 'always prints the last line of file?' sorry, I don't understand. $2 of last line is printed only if last line is in error or previous last line is in error.

    – ctac_
    Nov 13 '18 at 19:52











  • Hi catc, tks for the code and explanation, sorry for 'always prints the last line of file' in my file i had 2 extra empty lines at the end of file, that was the reason the code prints it.. But it is Ok now. Tks a lot

    – OXXO
    Nov 14 '18 at 5:49
















1














You can try this awk :



awk '
f { # if flag print ERROR
p = p FS "-->" FS $2
print "** ERROR **" FS p
split ( p , a ) # keep $2 of previous line in p
p = a[3] # this way if it is in error you can catch it
f = 0 # remove flag
}
$2 > p { # $2 is ok keep it
p = $2
next
}
{
p = p FS "-->" FS $2 # $2 is not ok
f = 1 # prepare to print
}
END {
if ( f ) # the last line is in error
print "** ERROR **" FS p # print it
}
' file





share|improve this answer
























  • hi ctac, the code works, but always prints the last line of file? also it considers as error if the previous value is the same as the actual..?.. Kindly can you fix it. Tks

    – OXXO
    Nov 13 '18 at 19:22











  • If the previous value is the same as the actual, it's not in increase order. You can change the line $2 > p by $2 >= p. 'always prints the last line of file?' sorry, I don't understand. $2 of last line is printed only if last line is in error or previous last line is in error.

    – ctac_
    Nov 13 '18 at 19:52











  • Hi catc, tks for the code and explanation, sorry for 'always prints the last line of file' in my file i had 2 extra empty lines at the end of file, that was the reason the code prints it.. But it is Ok now. Tks a lot

    – OXXO
    Nov 14 '18 at 5:49














1












1








1







You can try this awk :



awk '
f { # if flag print ERROR
p = p FS "-->" FS $2
print "** ERROR **" FS p
split ( p , a ) # keep $2 of previous line in p
p = a[3] # this way if it is in error you can catch it
f = 0 # remove flag
}
$2 > p { # $2 is ok keep it
p = $2
next
}
{
p = p FS "-->" FS $2 # $2 is not ok
f = 1 # prepare to print
}
END {
if ( f ) # the last line is in error
print "** ERROR **" FS p # print it
}
' file





share|improve this answer













You can try this awk :



awk '
f { # if flag print ERROR
p = p FS "-->" FS $2
print "** ERROR **" FS p
split ( p , a ) # keep $2 of previous line in p
p = a[3] # this way if it is in error you can catch it
f = 0 # remove flag
}
$2 > p { # $2 is ok keep it
p = $2
next
}
{
p = p FS "-->" FS $2 # $2 is not ok
f = 1 # prepare to print
}
END {
if ( f ) # the last line is in error
print "** ERROR **" FS p # print it
}
' file






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 17:55









ctac_ctac_

1,807138




1,807138













  • hi ctac, the code works, but always prints the last line of file? also it considers as error if the previous value is the same as the actual..?.. Kindly can you fix it. Tks

    – OXXO
    Nov 13 '18 at 19:22











  • If the previous value is the same as the actual, it's not in increase order. You can change the line $2 > p by $2 >= p. 'always prints the last line of file?' sorry, I don't understand. $2 of last line is printed only if last line is in error or previous last line is in error.

    – ctac_
    Nov 13 '18 at 19:52











  • Hi catc, tks for the code and explanation, sorry for 'always prints the last line of file' in my file i had 2 extra empty lines at the end of file, that was the reason the code prints it.. But it is Ok now. Tks a lot

    – OXXO
    Nov 14 '18 at 5:49



















  • hi ctac, the code works, but always prints the last line of file? also it considers as error if the previous value is the same as the actual..?.. Kindly can you fix it. Tks

    – OXXO
    Nov 13 '18 at 19:22











  • If the previous value is the same as the actual, it's not in increase order. You can change the line $2 > p by $2 >= p. 'always prints the last line of file?' sorry, I don't understand. $2 of last line is printed only if last line is in error or previous last line is in error.

    – ctac_
    Nov 13 '18 at 19:52











  • Hi catc, tks for the code and explanation, sorry for 'always prints the last line of file' in my file i had 2 extra empty lines at the end of file, that was the reason the code prints it.. But it is Ok now. Tks a lot

    – OXXO
    Nov 14 '18 at 5:49

















hi ctac, the code works, but always prints the last line of file? also it considers as error if the previous value is the same as the actual..?.. Kindly can you fix it. Tks

– OXXO
Nov 13 '18 at 19:22





hi ctac, the code works, but always prints the last line of file? also it considers as error if the previous value is the same as the actual..?.. Kindly can you fix it. Tks

– OXXO
Nov 13 '18 at 19:22













If the previous value is the same as the actual, it's not in increase order. You can change the line $2 > p by $2 >= p. 'always prints the last line of file?' sorry, I don't understand. $2 of last line is printed only if last line is in error or previous last line is in error.

– ctac_
Nov 13 '18 at 19:52





If the previous value is the same as the actual, it's not in increase order. You can change the line $2 > p by $2 >= p. 'always prints the last line of file?' sorry, I don't understand. $2 of last line is printed only if last line is in error or previous last line is in error.

– ctac_
Nov 13 '18 at 19:52













Hi catc, tks for the code and explanation, sorry for 'always prints the last line of file' in my file i had 2 extra empty lines at the end of file, that was the reason the code prints it.. But it is Ok now. Tks a lot

– OXXO
Nov 14 '18 at 5:49





Hi catc, tks for the code and explanation, sorry for 'always prints the last line of file' in my file i had 2 extra empty lines at the end of file, that was the reason the code prints it.. But it is Ok now. Tks a lot

– OXXO
Nov 14 '18 at 5:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f53284188%2fon-error-print-previous-actual-and-next-record%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python