How to use regex as field separator in awk?

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












12














I'm trying to use regex as a field seperator in awk. From my reading this seems possible but I can't get the syntax right.



rpm -qa | awk ' 'FS == [0-9]' ; print $1 '
awk: cmd. line:1: { FS
awk: cmd. line:1: ^ unexpected newline or end of string


Thoughts? The goal if not obviouse is to get a list of software without version number.










share|improve this question




























    12














    I'm trying to use regex as a field seperator in awk. From my reading this seems possible but I can't get the syntax right.



    rpm -qa | awk ' 'FS == [0-9]' ; print $1 '
    awk: cmd. line:1: { FS
    awk: cmd. line:1: ^ unexpected newline or end of string


    Thoughts? The goal if not obviouse is to get a list of software without version number.










    share|improve this question


























      12












      12








      12


      1





      I'm trying to use regex as a field seperator in awk. From my reading this seems possible but I can't get the syntax right.



      rpm -qa | awk ' 'FS == [0-9]' ; print $1 '
      awk: cmd. line:1: { FS
      awk: cmd. line:1: ^ unexpected newline or end of string


      Thoughts? The goal if not obviouse is to get a list of software without version number.










      share|improve this question















      I'm trying to use regex as a field seperator in awk. From my reading this seems possible but I can't get the syntax right.



      rpm -qa | awk ' 'FS == [0-9]' ; print $1 '
      awk: cmd. line:1: { FS
      awk: cmd. line:1: ^ unexpected newline or end of string


      Thoughts? The goal if not obviouse is to get a list of software without version number.







      regular-expression awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 8 '11 at 19:23









      bahamat

      24.2k14890




      24.2k14890










      asked Oct 7 '11 at 17:44









      Gray Race

      163115




      163115




















          2 Answers
          2






          active

          oldest

          votes


















          18














          You have mucked up your quotes and syntax. To set the input field separator, the easiest way to do it is with the -F option on the command line:



          awk -F '[0-9]' ' print $1 '


          or



          awk -F '[[:digit:]]' ' print $1 '


          This would use any digit as the input field separator, and then output the first field from each line.



          The [0-9] and [[:digit:]] expressions are not quite the same, depending on your locale. See "Difference between [0-9], [[:digit:]] and d".



          One could also set FS in the awk program itself. This is usually done in a BEGIN block as it's a one-time initialisation:



          awk 'BEGIN FS = "[0-9]" print $1 '


          Note that single quotes can't be used in a single-quoted string in the shell, and that awk strings always use double quotes.






          share|improve this answer






























            12














            +1 for KAK's answer. Alternately, the FS variable can be set in the BEGIN block:



            awk 'BEGIN FS="[0-9]" print $1'


            Changing FS in a action block won't take effect until the next line is read



            $ printf "%sn" "abc123 def456" "ghi789 jkl0" | awk 'FS="[0-9]"; print $1'
            abc123
            ghi


            The other errors in the question:



            • can't use single quotes inside a single-quoted string


            • == is a comparison operator, = is for variable assignment





            share|improve this answer




















            • "Changing FS in a action block won't take effect until the next line is read" I've been looking all over for that info.
              – Samizdis
              Jun 26 '17 at 15:50










            • plus: can't use single quotes for string value in awk, even if you pass them from shell correctly
              – dave_thompson_085
              Jun 27 '18 at 9:24










            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f22273%2fhow-to-use-regex-as-field-separator-in-awk%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            18














            You have mucked up your quotes and syntax. To set the input field separator, the easiest way to do it is with the -F option on the command line:



            awk -F '[0-9]' ' print $1 '


            or



            awk -F '[[:digit:]]' ' print $1 '


            This would use any digit as the input field separator, and then output the first field from each line.



            The [0-9] and [[:digit:]] expressions are not quite the same, depending on your locale. See "Difference between [0-9], [[:digit:]] and d".



            One could also set FS in the awk program itself. This is usually done in a BEGIN block as it's a one-time initialisation:



            awk 'BEGIN FS = "[0-9]" print $1 '


            Note that single quotes can't be used in a single-quoted string in the shell, and that awk strings always use double quotes.






            share|improve this answer



























              18














              You have mucked up your quotes and syntax. To set the input field separator, the easiest way to do it is with the -F option on the command line:



              awk -F '[0-9]' ' print $1 '


              or



              awk -F '[[:digit:]]' ' print $1 '


              This would use any digit as the input field separator, and then output the first field from each line.



              The [0-9] and [[:digit:]] expressions are not quite the same, depending on your locale. See "Difference between [0-9], [[:digit:]] and d".



              One could also set FS in the awk program itself. This is usually done in a BEGIN block as it's a one-time initialisation:



              awk 'BEGIN FS = "[0-9]" print $1 '


              Note that single quotes can't be used in a single-quoted string in the shell, and that awk strings always use double quotes.






              share|improve this answer

























                18












                18








                18






                You have mucked up your quotes and syntax. To set the input field separator, the easiest way to do it is with the -F option on the command line:



                awk -F '[0-9]' ' print $1 '


                or



                awk -F '[[:digit:]]' ' print $1 '


                This would use any digit as the input field separator, and then output the first field from each line.



                The [0-9] and [[:digit:]] expressions are not quite the same, depending on your locale. See "Difference between [0-9], [[:digit:]] and d".



                One could also set FS in the awk program itself. This is usually done in a BEGIN block as it's a one-time initialisation:



                awk 'BEGIN FS = "[0-9]" print $1 '


                Note that single quotes can't be used in a single-quoted string in the shell, and that awk strings always use double quotes.






                share|improve this answer














                You have mucked up your quotes and syntax. To set the input field separator, the easiest way to do it is with the -F option on the command line:



                awk -F '[0-9]' ' print $1 '


                or



                awk -F '[[:digit:]]' ' print $1 '


                This would use any digit as the input field separator, and then output the first field from each line.



                The [0-9] and [[:digit:]] expressions are not quite the same, depending on your locale. See "Difference between [0-9], [[:digit:]] and d".



                One could also set FS in the awk program itself. This is usually done in a BEGIN block as it's a one-time initialisation:



                awk 'BEGIN FS = "[0-9]" print $1 '


                Note that single quotes can't be used in a single-quoted string in the shell, and that awk strings always use double quotes.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 21 '18 at 9:45

























                answered Oct 7 '11 at 18:04









                Kusalananda

                122k16229374




                122k16229374























                    12














                    +1 for KAK's answer. Alternately, the FS variable can be set in the BEGIN block:



                    awk 'BEGIN FS="[0-9]" print $1'


                    Changing FS in a action block won't take effect until the next line is read



                    $ printf "%sn" "abc123 def456" "ghi789 jkl0" | awk 'FS="[0-9]"; print $1'
                    abc123
                    ghi


                    The other errors in the question:



                    • can't use single quotes inside a single-quoted string


                    • == is a comparison operator, = is for variable assignment





                    share|improve this answer




















                    • "Changing FS in a action block won't take effect until the next line is read" I've been looking all over for that info.
                      – Samizdis
                      Jun 26 '17 at 15:50










                    • plus: can't use single quotes for string value in awk, even if you pass them from shell correctly
                      – dave_thompson_085
                      Jun 27 '18 at 9:24















                    12














                    +1 for KAK's answer. Alternately, the FS variable can be set in the BEGIN block:



                    awk 'BEGIN FS="[0-9]" print $1'


                    Changing FS in a action block won't take effect until the next line is read



                    $ printf "%sn" "abc123 def456" "ghi789 jkl0" | awk 'FS="[0-9]"; print $1'
                    abc123
                    ghi


                    The other errors in the question:



                    • can't use single quotes inside a single-quoted string


                    • == is a comparison operator, = is for variable assignment





                    share|improve this answer




















                    • "Changing FS in a action block won't take effect until the next line is read" I've been looking all over for that info.
                      – Samizdis
                      Jun 26 '17 at 15:50










                    • plus: can't use single quotes for string value in awk, even if you pass them from shell correctly
                      – dave_thompson_085
                      Jun 27 '18 at 9:24













                    12












                    12








                    12






                    +1 for KAK's answer. Alternately, the FS variable can be set in the BEGIN block:



                    awk 'BEGIN FS="[0-9]" print $1'


                    Changing FS in a action block won't take effect until the next line is read



                    $ printf "%sn" "abc123 def456" "ghi789 jkl0" | awk 'FS="[0-9]"; print $1'
                    abc123
                    ghi


                    The other errors in the question:



                    • can't use single quotes inside a single-quoted string


                    • == is a comparison operator, = is for variable assignment





                    share|improve this answer












                    +1 for KAK's answer. Alternately, the FS variable can be set in the BEGIN block:



                    awk 'BEGIN FS="[0-9]" print $1'


                    Changing FS in a action block won't take effect until the next line is read



                    $ printf "%sn" "abc123 def456" "ghi789 jkl0" | awk 'FS="[0-9]"; print $1'
                    abc123
                    ghi


                    The other errors in the question:



                    • can't use single quotes inside a single-quoted string


                    • == is a comparison operator, = is for variable assignment






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 7 '11 at 18:20









                    glenn jackman

                    50.3k570107




                    50.3k570107











                    • "Changing FS in a action block won't take effect until the next line is read" I've been looking all over for that info.
                      – Samizdis
                      Jun 26 '17 at 15:50










                    • plus: can't use single quotes for string value in awk, even if you pass them from shell correctly
                      – dave_thompson_085
                      Jun 27 '18 at 9:24
















                    • "Changing FS in a action block won't take effect until the next line is read" I've been looking all over for that info.
                      – Samizdis
                      Jun 26 '17 at 15:50










                    • plus: can't use single quotes for string value in awk, even if you pass them from shell correctly
                      – dave_thompson_085
                      Jun 27 '18 at 9:24















                    "Changing FS in a action block won't take effect until the next line is read" I've been looking all over for that info.
                    – Samizdis
                    Jun 26 '17 at 15:50




                    "Changing FS in a action block won't take effect until the next line is read" I've been looking all over for that info.
                    – Samizdis
                    Jun 26 '17 at 15:50












                    plus: can't use single quotes for string value in awk, even if you pass them from shell correctly
                    – dave_thompson_085
                    Jun 27 '18 at 9:24




                    plus: can't use single quotes for string value in awk, even if you pass them from shell correctly
                    – dave_thompson_085
                    Jun 27 '18 at 9:24

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f22273%2fhow-to-use-regex-as-field-separator-in-awk%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

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?