How to grep for unicode � in a bash script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












if grep -q "�" out.txt
then
echo "working"
else
cat out.txt
fi


Basically, if the file "out.txt" contains "�" anywhere in the file I would like it to echo "working" AND if the file "out.txt" does NOT contain "�" anywhere in the file then I would like it to cat out.txt










share|improve this question







New contributor




Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • It looks correct, it should work (btw, I have no font for your unicode character to see, but none of them has any special meaning). grep long understands unicode (which makes it much slower, so to search for ascii strings, a LANG=C grep is a huge performance improvement).
    – peterh
    22 mins ago















up vote
2
down vote

favorite












if grep -q "�" out.txt
then
echo "working"
else
cat out.txt
fi


Basically, if the file "out.txt" contains "�" anywhere in the file I would like it to echo "working" AND if the file "out.txt" does NOT contain "�" anywhere in the file then I would like it to cat out.txt










share|improve this question







New contributor




Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • It looks correct, it should work (btw, I have no font for your unicode character to see, but none of them has any special meaning). grep long understands unicode (which makes it much slower, so to search for ascii strings, a LANG=C grep is a huge performance improvement).
    – peterh
    22 mins ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











if grep -q "�" out.txt
then
echo "working"
else
cat out.txt
fi


Basically, if the file "out.txt" contains "�" anywhere in the file I would like it to echo "working" AND if the file "out.txt" does NOT contain "�" anywhere in the file then I would like it to cat out.txt










share|improve this question







New contributor




Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











if grep -q "�" out.txt
then
echo "working"
else
cat out.txt
fi


Basically, if the file "out.txt" contains "�" anywhere in the file I would like it to echo "working" AND if the file "out.txt" does NOT contain "�" anywhere in the file then I would like it to cat out.txt







linux bash grep scripting






share|improve this question







New contributor




Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 31 mins ago









Stuart Sloan

112




112




New contributor




Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Stuart Sloan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • It looks correct, it should work (btw, I have no font for your unicode character to see, but none of them has any special meaning). grep long understands unicode (which makes it much slower, so to search for ascii strings, a LANG=C grep is a huge performance improvement).
    – peterh
    22 mins ago

















  • It looks correct, it should work (btw, I have no font for your unicode character to see, but none of them has any special meaning). grep long understands unicode (which makes it much slower, so to search for ascii strings, a LANG=C grep is a huge performance improvement).
    – peterh
    22 mins ago
















It looks correct, it should work (btw, I have no font for your unicode character to see, but none of them has any special meaning). grep long understands unicode (which makes it much slower, so to search for ascii strings, a LANG=C grep is a huge performance improvement).
– peterh
22 mins ago





It looks correct, it should work (btw, I have no font for your unicode character to see, but none of them has any special meaning). grep long understands unicode (which makes it much slower, so to search for ascii strings, a LANG=C grep is a huge performance improvement).
– peterh
22 mins ago











1 Answer
1






active

oldest

votes

















up vote
5
down vote













try



grep -oP "[^x00-x7F]"


if .. then statement as follows:



if grep -oP "[^x00-x7F]" file.txt; then
echo "grep found something"
else
echo "not found"
fi


Explanation💡:



[^x00-x7F] is a regex to match a single non-ASCII character.
[[:ascii:]] - matches a single ASCII char
[^[:ascii:]] - matches a single non-ASCII char


in bash



LC_COLLATE=C grep -o '[^ -~]' file





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Stuart Sloan is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474709%2fhow-to-grep-for-unicode-in-a-bash-script%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    try



    grep -oP "[^x00-x7F]"


    if .. then statement as follows:



    if grep -oP "[^x00-x7F]" file.txt; then
    echo "grep found something"
    else
    echo "not found"
    fi


    Explanation💡:



    [^x00-x7F] is a regex to match a single non-ASCII character.
    [[:ascii:]] - matches a single ASCII char
    [^[:ascii:]] - matches a single non-ASCII char


    in bash



    LC_COLLATE=C grep -o '[^ -~]' file





    share|improve this answer


























      up vote
      5
      down vote













      try



      grep -oP "[^x00-x7F]"


      if .. then statement as follows:



      if grep -oP "[^x00-x7F]" file.txt; then
      echo "grep found something"
      else
      echo "not found"
      fi


      Explanation💡:



      [^x00-x7F] is a regex to match a single non-ASCII character.
      [[:ascii:]] - matches a single ASCII char
      [^[:ascii:]] - matches a single non-ASCII char


      in bash



      LC_COLLATE=C grep -o '[^ -~]' file





      share|improve this answer
























        up vote
        5
        down vote










        up vote
        5
        down vote









        try



        grep -oP "[^x00-x7F]"


        if .. then statement as follows:



        if grep -oP "[^x00-x7F]" file.txt; then
        echo "grep found something"
        else
        echo "not found"
        fi


        Explanation💡:



        [^x00-x7F] is a regex to match a single non-ASCII character.
        [[:ascii:]] - matches a single ASCII char
        [^[:ascii:]] - matches a single non-ASCII char


        in bash



        LC_COLLATE=C grep -o '[^ -~]' file





        share|improve this answer














        try



        grep -oP "[^x00-x7F]"


        if .. then statement as follows:



        if grep -oP "[^x00-x7F]" file.txt; then
        echo "grep found something"
        else
        echo "not found"
        fi


        Explanation💡:



        [^x00-x7F] is a regex to match a single non-ASCII character.
        [[:ascii:]] - matches a single ASCII char
        [^[:ascii:]] - matches a single non-ASCII char


        in bash



        LC_COLLATE=C grep -o '[^ -~]' file






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 3 mins ago

























        answered 23 mins ago









        Goro

        8,38054282




        8,38054282




















            Stuart Sloan is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Stuart Sloan is a new contributor. Be nice, and check out our Code of Conduct.












            Stuart Sloan is a new contributor. Be nice, and check out our Code of Conduct.











            Stuart Sloan is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474709%2fhow-to-grep-for-unicode-in-a-bash-script%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery