Extract and process linenumber from grep due to an error in windows (because the extracted number is a...












0

















I have a problem with extracted linenumbers from a grep result.
Windows (gitBash) can't compare them, because the number is a string and not a number (my macOS does it without problems).




What i want to do is: add a leading zero to the line numbers if they are smaller than 10


Here is the code snippet:

local number=""
local command=""
# grep complete list and itereate over this list
grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do
for ln in "${greppedList}" ; do
# split the line to number and command
number=$(echo ${ln} | cut -d ':' -f 1)
if (( ${number} < 10 )) ; then
command="${ln:2:${#ln}-1}"
else
command="${ln:3:${#ln}-1}"
fi
printWithFormattedLineNumbers "${number}" "${command}"
done
done



The problem on windows is that line if (( ${number} < 10 )) ; then.

windows can't compare it because it is not number.



Can you help me changing the way to get the linenumber?










share|improve this question






















  • I don't know if it will work in Windows, but you could try this while IFS=':' read num line;do printf 'number %02d - line %sn' $num "$line";done.
    – Paulo
    Sep 5 '17 at 13:58










  • i had problems with while IFS=':' read num line, that didn't work if there was only one line. But the most important fact was %02d - line %sn' $num "$line" - that works perfectly. Thank you!
    – m1well
    Sep 7 '17 at 5:32










  • If passing IFS to read didn't work, try doing the cut directly as printf arguments grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do printf 'number %02d - line %sn' "$(cut -d':' -f1 <<<"$greppedList")" "$(cut -d':' -f2- <<<"$greppedList")" ; done. I think the printf line replaces the for loop.
    – Paulo
    Sep 7 '17 at 23:09












  • yes that also works, but is a very long line :D thanky you for your help!
    – m1well
    Sep 8 '17 at 5:43










  • you can find on github github.com/m1well/cheatsheet at the script on line 261 an 270 your code now ;)
    – m1well
    Sep 8 '17 at 5:45


















0

















I have a problem with extracted linenumbers from a grep result.
Windows (gitBash) can't compare them, because the number is a string and not a number (my macOS does it without problems).




What i want to do is: add a leading zero to the line numbers if they are smaller than 10


Here is the code snippet:

local number=""
local command=""
# grep complete list and itereate over this list
grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do
for ln in "${greppedList}" ; do
# split the line to number and command
number=$(echo ${ln} | cut -d ':' -f 1)
if (( ${number} < 10 )) ; then
command="${ln:2:${#ln}-1}"
else
command="${ln:3:${#ln}-1}"
fi
printWithFormattedLineNumbers "${number}" "${command}"
done
done



The problem on windows is that line if (( ${number} < 10 )) ; then.

windows can't compare it because it is not number.



Can you help me changing the way to get the linenumber?










share|improve this question






















  • I don't know if it will work in Windows, but you could try this while IFS=':' read num line;do printf 'number %02d - line %sn' $num "$line";done.
    – Paulo
    Sep 5 '17 at 13:58










  • i had problems with while IFS=':' read num line, that didn't work if there was only one line. But the most important fact was %02d - line %sn' $num "$line" - that works perfectly. Thank you!
    – m1well
    Sep 7 '17 at 5:32










  • If passing IFS to read didn't work, try doing the cut directly as printf arguments grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do printf 'number %02d - line %sn' "$(cut -d':' -f1 <<<"$greppedList")" "$(cut -d':' -f2- <<<"$greppedList")" ; done. I think the printf line replaces the for loop.
    – Paulo
    Sep 7 '17 at 23:09












  • yes that also works, but is a very long line :D thanky you for your help!
    – m1well
    Sep 8 '17 at 5:43










  • you can find on github github.com/m1well/cheatsheet at the script on line 261 an 270 your code now ;)
    – m1well
    Sep 8 '17 at 5:45
















0












0








0










I have a problem with extracted linenumbers from a grep result.
Windows (gitBash) can't compare them, because the number is a string and not a number (my macOS does it without problems).




What i want to do is: add a leading zero to the line numbers if they are smaller than 10


Here is the code snippet:

local number=""
local command=""
# grep complete list and itereate over this list
grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do
for ln in "${greppedList}" ; do
# split the line to number and command
number=$(echo ${ln} | cut -d ':' -f 1)
if (( ${number} < 10 )) ; then
command="${ln:2:${#ln}-1}"
else
command="${ln:3:${#ln}-1}"
fi
printWithFormattedLineNumbers "${number}" "${command}"
done
done



The problem on windows is that line if (( ${number} < 10 )) ; then.

windows can't compare it because it is not number.



Can you help me changing the way to get the linenumber?










share|improve this question
















I have a problem with extracted linenumbers from a grep result.
Windows (gitBash) can't compare them, because the number is a string and not a number (my macOS does it without problems).




What i want to do is: add a leading zero to the line numbers if they are smaller than 10


Here is the code snippet:

local number=""
local command=""
# grep complete list and itereate over this list
grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do
for ln in "${greppedList}" ; do
# split the line to number and command
number=$(echo ${ln} | cut -d ':' -f 1)
if (( ${number} < 10 )) ; then
command="${ln:2:${#ln}-1}"
else
command="${ln:3:${#ln}-1}"
fi
printWithFormattedLineNumbers "${number}" "${command}"
done
done



The problem on windows is that line if (( ${number} < 10 )) ; then.

windows can't compare it because it is not number.



Can you help me changing the way to get the linenumber?







command-line bash grep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 5 '17 at 6:41









m1well

135




135












  • I don't know if it will work in Windows, but you could try this while IFS=':' read num line;do printf 'number %02d - line %sn' $num "$line";done.
    – Paulo
    Sep 5 '17 at 13:58










  • i had problems with while IFS=':' read num line, that didn't work if there was only one line. But the most important fact was %02d - line %sn' $num "$line" - that works perfectly. Thank you!
    – m1well
    Sep 7 '17 at 5:32










  • If passing IFS to read didn't work, try doing the cut directly as printf arguments grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do printf 'number %02d - line %sn' "$(cut -d':' -f1 <<<"$greppedList")" "$(cut -d':' -f2- <<<"$greppedList")" ; done. I think the printf line replaces the for loop.
    – Paulo
    Sep 7 '17 at 23:09












  • yes that also works, but is a very long line :D thanky you for your help!
    – m1well
    Sep 8 '17 at 5:43










  • you can find on github github.com/m1well/cheatsheet at the script on line 261 an 270 your code now ;)
    – m1well
    Sep 8 '17 at 5:45




















  • I don't know if it will work in Windows, but you could try this while IFS=':' read num line;do printf 'number %02d - line %sn' $num "$line";done.
    – Paulo
    Sep 5 '17 at 13:58










  • i had problems with while IFS=':' read num line, that didn't work if there was only one line. But the most important fact was %02d - line %sn' $num "$line" - that works perfectly. Thank you!
    – m1well
    Sep 7 '17 at 5:32










  • If passing IFS to read didn't work, try doing the cut directly as printf arguments grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do printf 'number %02d - line %sn' "$(cut -d':' -f1 <<<"$greppedList")" "$(cut -d':' -f2- <<<"$greppedList")" ; done. I think the printf line replaces the for loop.
    – Paulo
    Sep 7 '17 at 23:09












  • yes that also works, but is a very long line :D thanky you for your help!
    – m1well
    Sep 8 '17 at 5:43










  • you can find on github github.com/m1well/cheatsheet at the script on line 261 an 270 your code now ;)
    – m1well
    Sep 8 '17 at 5:45


















I don't know if it will work in Windows, but you could try this while IFS=':' read num line;do printf 'number %02d - line %sn' $num "$line";done.
– Paulo
Sep 5 '17 at 13:58




I don't know if it will work in Windows, but you could try this while IFS=':' read num line;do printf 'number %02d - line %sn' $num "$line";done.
– Paulo
Sep 5 '17 at 13:58












i had problems with while IFS=':' read num line, that didn't work if there was only one line. But the most important fact was %02d - line %sn' $num "$line" - that works perfectly. Thank you!
– m1well
Sep 7 '17 at 5:32




i had problems with while IFS=':' read num line, that didn't work if there was only one line. But the most important fact was %02d - line %sn' $num "$line" - that works perfectly. Thank you!
– m1well
Sep 7 '17 at 5:32












If passing IFS to read didn't work, try doing the cut directly as printf arguments grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do printf 'number %02d - line %sn' "$(cut -d':' -f1 <<<"$greppedList")" "$(cut -d':' -f2- <<<"$greppedList")" ; done. I think the printf line replaces the for loop.
– Paulo
Sep 7 '17 at 23:09






If passing IFS to read didn't work, try doing the cut directly as printf arguments grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do printf 'number %02d - line %sn' "$(cut -d':' -f1 <<<"$greppedList")" "$(cut -d':' -f2- <<<"$greppedList")" ; done. I think the printf line replaces the for loop.
– Paulo
Sep 7 '17 at 23:09














yes that also works, but is a very long line :D thanky you for your help!
– m1well
Sep 8 '17 at 5:43




yes that also works, but is a very long line :D thanky you for your help!
– m1well
Sep 8 '17 at 5:43












you can find on github github.com/m1well/cheatsheet at the script on line 261 an 270 your code now ;)
– m1well
Sep 8 '17 at 5:45






you can find on github github.com/m1well/cheatsheet at the script on line 261 an 270 your code now ;)
– m1well
Sep 8 '17 at 5:45












1 Answer
1






active

oldest

votes


















0















The problem on windows is that line if (( ${number} < 10 )) ; then.
windows can't compare it because it is not number.




Then treat it as a character string, and use a case statement instead of if:



case "${number}" in
[0-9])
command="${ln:2:${#ln}-1}"
;;
*)
command="${ln:3:${#ln}-1}"
esac





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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%2fsuperuser.com%2fquestions%2f1247452%2fextract-and-process-linenumber-from-grep-due-to-an-error-in-windows-because-the%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















    The problem on windows is that line if (( ${number} < 10 )) ; then.
    windows can't compare it because it is not number.




    Then treat it as a character string, and use a case statement instead of if:



    case "${number}" in
    [0-9])
    command="${ln:2:${#ln}-1}"
    ;;
    *)
    command="${ln:3:${#ln}-1}"
    esac





    share|improve this answer


























      0















      The problem on windows is that line if (( ${number} < 10 )) ; then.
      windows can't compare it because it is not number.




      Then treat it as a character string, and use a case statement instead of if:



      case "${number}" in
      [0-9])
      command="${ln:2:${#ln}-1}"
      ;;
      *)
      command="${ln:3:${#ln}-1}"
      esac





      share|improve this answer
























        0












        0








        0







        The problem on windows is that line if (( ${number} < 10 )) ; then.
        windows can't compare it because it is not number.




        Then treat it as a character string, and use a case statement instead of if:



        case "${number}" in
        [0-9])
        command="${ln:2:${#ln}-1}"
        ;;
        *)
        command="${ln:3:${#ln}-1}"
        esac





        share|improve this answer













        The problem on windows is that line if (( ${number} < 10 )) ; then.
        windows can't compare it because it is not number.




        Then treat it as a character string, and use a case statement instead of if:



        case "${number}" in
        [0-9])
        command="${ln:2:${#ln}-1}"
        ;;
        *)
        command="${ln:3:${#ln}-1}"
        esac






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 14 at 0:02









        Jim L.

        913




        913






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fsuperuser.com%2fquestions%2f1247452%2fextract-and-process-linenumber-from-grep-due-to-an-error-in-windows-because-the%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

            Index of /

            Tribalistas

            Listed building