Two field separators (colon and space) in awkInconsistent field separator behaviour of space in awkAwk field...

Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?

Why do games have consumables?

Converting a sprinkler system's 24V AC outputs to 3.3V DC logic inputs

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

infinitely many negative and infinitely many positive numbers

Two field separators (colon and space) in awk

Is there really no use for MD5 anymore?

Pre-plastic human skin alternative

Apply MapThread to all but one variable

Rivers without rain

Is it idiomatic to construct against `this`

Multiple options vs single option UI

Is Diceware more secure than a long passphrase?

Does tea made with boiling water cool faster than tea made with boiled (but still hot) water?

Which big number is bigger?

Was there a Viking Exchange as well as a Columbian one?

How to have a sharp product image?

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

How do I reattach a shelf to the wall when it ripped out of the wall?

On The Origin of Dissonant Chords

How can I practically buy stocks?

Classification of surfaces

What happened to Captain America in Endgame?



Two field separators (colon and space) in awk


Inconsistent field separator behaviour of space in awkAwk field separator bug?Run unix command on awk fieldAWK : the ' ' as field separatorParsing with awk by modifying the field and record separatorscompare files basis two columns and add fieldawk won't use '||' as a field delimiterHow to delete input field in AWK?awk: How to use different field separators, and operate on different columnsAWK fields filtering misses the first field






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







8















How can we specify two field separators in awk command when one of them is space? I think this can be done by using an embedded if-else condition in awk, however I am not sure about the exact command.



Sample file is as below:



cat test.txt
Swapnil Engineer 20000
Avinash:Doctor:30000
Dattu GovntJob 50000
Amol:Master:10000


I want to print the second column ($2). Expected output is:



Engineer
Doctor
GovntJob
Master


When I tried to put both space and colon as field seperators, it failed with a syntax error:



awk -F[ :] '{print $2}' test.txt
awk: cmd. line:1: :]
awk: cmd. line:1: ^ syntax error


How can we use two field separators and utilize awk functionality?










share|improve this question































    8















    How can we specify two field separators in awk command when one of them is space? I think this can be done by using an embedded if-else condition in awk, however I am not sure about the exact command.



    Sample file is as below:



    cat test.txt
    Swapnil Engineer 20000
    Avinash:Doctor:30000
    Dattu GovntJob 50000
    Amol:Master:10000


    I want to print the second column ($2). Expected output is:



    Engineer
    Doctor
    GovntJob
    Master


    When I tried to put both space and colon as field seperators, it failed with a syntax error:



    awk -F[ :] '{print $2}' test.txt
    awk: cmd. line:1: :]
    awk: cmd. line:1: ^ syntax error


    How can we use two field separators and utilize awk functionality?










    share|improve this question



























      8












      8








      8


      1






      How can we specify two field separators in awk command when one of them is space? I think this can be done by using an embedded if-else condition in awk, however I am not sure about the exact command.



      Sample file is as below:



      cat test.txt
      Swapnil Engineer 20000
      Avinash:Doctor:30000
      Dattu GovntJob 50000
      Amol:Master:10000


      I want to print the second column ($2). Expected output is:



      Engineer
      Doctor
      GovntJob
      Master


      When I tried to put both space and colon as field seperators, it failed with a syntax error:



      awk -F[ :] '{print $2}' test.txt
      awk: cmd. line:1: :]
      awk: cmd. line:1: ^ syntax error


      How can we use two field separators and utilize awk functionality?










      share|improve this question
















      How can we specify two field separators in awk command when one of them is space? I think this can be done by using an embedded if-else condition in awk, however I am not sure about the exact command.



      Sample file is as below:



      cat test.txt
      Swapnil Engineer 20000
      Avinash:Doctor:30000
      Dattu GovntJob 50000
      Amol:Master:10000


      I want to print the second column ($2). Expected output is:



      Engineer
      Doctor
      GovntJob
      Master


      When I tried to put both space and colon as field seperators, it failed with a syntax error:



      awk -F[ :] '{print $2}' test.txt
      awk: cmd. line:1: :]
      awk: cmd. line:1: ^ syntax error


      How can we use two field separators and utilize awk functionality?







      awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 hours ago









      Jeff Schaller

      45.4k1164147




      45.4k1164147










      asked 17 hours ago









      Swapnil DhuleSwapnil Dhule

      855




      855






















          1 Answer
          1






          active

          oldest

          votes


















          12














          You are on the right track!

          Just add the missing quotes around [ :]:



          awk -F'[ :]' '{print $2}' test.txt
          Engineer
          Doctor
          GovntJob
          Master





          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',
            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%2f515678%2ftwo-field-separators-colon-and-space-in-awk%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









            12














            You are on the right track!

            Just add the missing quotes around [ :]:



            awk -F'[ :]' '{print $2}' test.txt
            Engineer
            Doctor
            GovntJob
            Master





            share|improve this answer






























              12














              You are on the right track!

              Just add the missing quotes around [ :]:



              awk -F'[ :]' '{print $2}' test.txt
              Engineer
              Doctor
              GovntJob
              Master





              share|improve this answer




























                12












                12








                12







                You are on the right track!

                Just add the missing quotes around [ :]:



                awk -F'[ :]' '{print $2}' test.txt
                Engineer
                Doctor
                GovntJob
                Master





                share|improve this answer















                You are on the right track!

                Just add the missing quotes around [ :]:



                awk -F'[ :]' '{print $2}' test.txt
                Engineer
                Doctor
                GovntJob
                Master






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 16 hours ago









                Swapnil Dhule

                855




                855










                answered 17 hours ago









                yetiyeti

                2,54111527




                2,54111527






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f515678%2ftwo-field-separators-colon-and-space-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

                    Why do type traits not work with types in namespace scope?What are POD types in C++?Why can templates only be...

                    Will tsunami waves travel forever if there was no land?Why do tsunami waves begin with the water flowing away...

                    Simple Scan not detecting my scanner (Brother DCP-7055W)Brother MFC-L2700DW printer can print, can't...