Setting multiples out of order variables with patternSetting environment variables on OS XHow do I iterate over a range of numbers defined by variables in Bash?How do you use a variable in a regular expression?How do I split a string on a delimiter in Bash?How do I reload .bashrc without logging out and back in?How to check if a variable is set in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?bash - parsing file and getting variableshow to cut the pattern from a multiple line string and store to the new variable?

Is it acceptable to say that a reviewer's concern is not going to be addressed because then the paper would be too long?

Do we have any particular tonal center in mind when we are NOT listening music?

List of 1000 most common words across all languages

Could Apollo astronauts see city lights from the moon?

Is differentiation as a map discontinuous?

Reorder a matrix, twice

Is there a way to hide HTML source code yet keeping it effective?

Can anyone put a name to this Circle of Fifths observation?

Can I take new (still in their boxes) PC parts in my checked in luggage?

Should the average user with no special access rights be worried about SMS-based 2FA being theoretically interceptable?

A file manager to open a zip file like opening a folder, instead of extract it by using a archive manager

Is it impolite to ask for halal food when traveling to and in Thailand?

He drink only five liquids

Why is a road bike faster than a city bike with the same effort? & how much faster it can be?

How can this Stack Exchange site have an animated favicon?

How to clarify between imagined sensations and "real" fantasy events?

Co-supervisor comes to the office to help her students, which distracts me

How to deal with a Homophobic PC

Does "as soon as" imply simultaneity?

Aesthetic proofs that involve Field Theory / Galois Theory

Is it allowed to buy a Probe Bahncard 50 repeatedly?

Why does NASA publish all the results/data it gets?

Does wetting a beer glass change the foam characteristics?

Why are there two fundamental laws of logic?



Setting multiples out of order variables with pattern


Setting environment variables on OS XHow do I iterate over a range of numbers defined by variables in Bash?How do you use a variable in a regular expression?How do I split a string on a delimiter in Bash?How do I reload .bashrc without logging out and back in?How to check if a variable is set in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?bash - parsing file and getting variableshow to cut the pattern from a multiple line string and store to the new variable?






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








1















I have a file in the following format (note that some lines have the variables inverted, in the example, line 3):



var1: valueA var2: valueB
var1: valueC var2: valueD
var2: valueF var1: valueE
var1: valueG var2: valueH


And I'm trying to use awk to print an output in the format (var1|var2):



valueA|valueB
valueC|valueD
valueE|valueF
valueG|valueH


I've tried to split and set the value based on patterns like this:



$ awk -v OFS='|' 'split($0,a," ") /^var1:/var1=a[2] /^var2:/var2=a[2] print var1, var2' test.txt
valueA|
valueC|
valueC|valueF
valueG|valueF


But all tries that I did ending with missing results or duplicated values.
Is there any way to set and print the variables based on the variables names (patterns)?










share|improve this question
































    1















    I have a file in the following format (note that some lines have the variables inverted, in the example, line 3):



    var1: valueA var2: valueB
    var1: valueC var2: valueD
    var2: valueF var1: valueE
    var1: valueG var2: valueH


    And I'm trying to use awk to print an output in the format (var1|var2):



    valueA|valueB
    valueC|valueD
    valueE|valueF
    valueG|valueH


    I've tried to split and set the value based on patterns like this:



    $ awk -v OFS='|' 'split($0,a," ") /^var1:/var1=a[2] /^var2:/var2=a[2] print var1, var2' test.txt
    valueA|
    valueC|
    valueC|valueF
    valueG|valueF


    But all tries that I did ending with missing results or duplicated values.
    Is there any way to set and print the variables based on the variables names (patterns)?










    share|improve this question




























      1












      1








      1








      I have a file in the following format (note that some lines have the variables inverted, in the example, line 3):



      var1: valueA var2: valueB
      var1: valueC var2: valueD
      var2: valueF var1: valueE
      var1: valueG var2: valueH


      And I'm trying to use awk to print an output in the format (var1|var2):



      valueA|valueB
      valueC|valueD
      valueE|valueF
      valueG|valueH


      I've tried to split and set the value based on patterns like this:



      $ awk -v OFS='|' 'split($0,a," ") /^var1:/var1=a[2] /^var2:/var2=a[2] print var1, var2' test.txt
      valueA|
      valueC|
      valueC|valueF
      valueG|valueF


      But all tries that I did ending with missing results or duplicated values.
      Is there any way to set and print the variables based on the variables names (patterns)?










      share|improve this question
















      I have a file in the following format (note that some lines have the variables inverted, in the example, line 3):



      var1: valueA var2: valueB
      var1: valueC var2: valueD
      var2: valueF var1: valueE
      var1: valueG var2: valueH


      And I'm trying to use awk to print an output in the format (var1|var2):



      valueA|valueB
      valueC|valueD
      valueE|valueF
      valueG|valueH


      I've tried to split and set the value based on patterns like this:



      $ awk -v OFS='|' 'split($0,a," ") /^var1:/var1=a[2] /^var2:/var2=a[2] print var1, var2' test.txt
      valueA|
      valueC|
      valueC|valueF
      valueG|valueF


      But all tries that I did ending with missing results or duplicated values.
      Is there any way to set and print the variables based on the variables names (patterns)?







      regex bash awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 28 at 17:42









      oguz ismail

      12.8k7 gold badges19 silver badges37 bronze badges




      12.8k7 gold badges19 silver badges37 bronze badges










      asked Mar 28 at 17:39









      ajkajk

      156 bronze badges




      156 bronze badges

























          4 Answers
          4






          active

          oldest

          votes


















          2
















          Any time you have name=value pairs like you do I find it best to first create an array of those pairings (f[] below) and then you can just access the value(s) by their name(s), e.g.:



          $ awk -F'[: ]+' -v OFS='|' 'for (i=1;i<NF;i+=2) f[$i]=$(i+1); print f["var1"], f["var2"]' file
          valueA|valueB
          valueC|valueD
          valueE|valueF
          valueG|valueH





          share|improve this answer




















          • 1





            Worked great! Can you explain how f[$i]=$(i+1) works? It creates an array and appends the values based on the separator :?

            – ajk
            Mar 28 at 17:49











          • When i is 1 i+1 is 2 so when i is 1 $i is var1 and $(i+1) is valueA. So you get an array populated as f["var1"] = "valueA", etc. as you go through the loop.

            – Ed Morton
            Mar 28 at 17:52



















          1
















          This is not a better answer, but it's fun:



          grep -oP 'w+:s+(.*?)(?=s+w+:|$)' file |
          sort --stable -t: -k1,1 |
          cut -d" " -f2- |
          pr -2T -s"|"





          share|improve this answer


































            0
















            could you please try following.



            awk '
            match($0,/var1: [^ ]*/)
            val=substr($0,RSTART+6,RLENGTH-6)
            match($0,/var2: [^ ]*/)
            print val"' Input_file


            OR(since first code will have a limitation it will look for val1 string should be present as well as string val2 too, so to avoid that one could try):



            awk '
            match($0,/var1: [^ ]*/)
            val=substr($0,RSTART+6,RLENGTH-6)

            match($0,/var2: [^ ]*/)
            print val"' Input_file


            Output will be as follows.



            valueA|valueB
            valueC|valueD
            valueE|valueF
            valueG|valueH





            share|improve this answer


































              0
















              Using Perl



              perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv) 
              " print "";$s="" ' file


              with your given inputs



              $ cat ajk.txt
              var1: valueA var2: valueB
              var1: valueC var2: valueD
              var2: valueF var1: valueE
              var1: valueG var2: valueH

              $ perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv)
              " print "";$s="" ' ajk.txt
              valueA|valueB
              valueC|valueD
              valueE|valueF
              valueG|valueH

              $





              share|improve this answer



























                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/4.0/"u003ecc by-sa 4.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%2f55403827%2fsetting-multiples-out-of-order-variables-with-pattern%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2
















                Any time you have name=value pairs like you do I find it best to first create an array of those pairings (f[] below) and then you can just access the value(s) by their name(s), e.g.:



                $ awk -F'[: ]+' -v OFS='|' 'for (i=1;i<NF;i+=2) f[$i]=$(i+1); print f["var1"], f["var2"]' file
                valueA|valueB
                valueC|valueD
                valueE|valueF
                valueG|valueH





                share|improve this answer




















                • 1





                  Worked great! Can you explain how f[$i]=$(i+1) works? It creates an array and appends the values based on the separator :?

                  – ajk
                  Mar 28 at 17:49











                • When i is 1 i+1 is 2 so when i is 1 $i is var1 and $(i+1) is valueA. So you get an array populated as f["var1"] = "valueA", etc. as you go through the loop.

                  – Ed Morton
                  Mar 28 at 17:52
















                2
















                Any time you have name=value pairs like you do I find it best to first create an array of those pairings (f[] below) and then you can just access the value(s) by their name(s), e.g.:



                $ awk -F'[: ]+' -v OFS='|' 'for (i=1;i<NF;i+=2) f[$i]=$(i+1); print f["var1"], f["var2"]' file
                valueA|valueB
                valueC|valueD
                valueE|valueF
                valueG|valueH





                share|improve this answer




















                • 1





                  Worked great! Can you explain how f[$i]=$(i+1) works? It creates an array and appends the values based on the separator :?

                  – ajk
                  Mar 28 at 17:49











                • When i is 1 i+1 is 2 so when i is 1 $i is var1 and $(i+1) is valueA. So you get an array populated as f["var1"] = "valueA", etc. as you go through the loop.

                  – Ed Morton
                  Mar 28 at 17:52














                2














                2










                2









                Any time you have name=value pairs like you do I find it best to first create an array of those pairings (f[] below) and then you can just access the value(s) by their name(s), e.g.:



                $ awk -F'[: ]+' -v OFS='|' 'for (i=1;i<NF;i+=2) f[$i]=$(i+1); print f["var1"], f["var2"]' file
                valueA|valueB
                valueC|valueD
                valueE|valueF
                valueG|valueH





                share|improve this answer













                Any time you have name=value pairs like you do I find it best to first create an array of those pairings (f[] below) and then you can just access the value(s) by their name(s), e.g.:



                $ awk -F'[: ]+' -v OFS='|' 'for (i=1;i<NF;i+=2) f[$i]=$(i+1); print f["var1"], f["var2"]' file
                valueA|valueB
                valueC|valueD
                valueE|valueF
                valueG|valueH






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 28 at 17:42









                Ed MortonEd Morton

                125k13 gold badges49 silver badges111 bronze badges




                125k13 gold badges49 silver badges111 bronze badges










                • 1





                  Worked great! Can you explain how f[$i]=$(i+1) works? It creates an array and appends the values based on the separator :?

                  – ajk
                  Mar 28 at 17:49











                • When i is 1 i+1 is 2 so when i is 1 $i is var1 and $(i+1) is valueA. So you get an array populated as f["var1"] = "valueA", etc. as you go through the loop.

                  – Ed Morton
                  Mar 28 at 17:52













                • 1





                  Worked great! Can you explain how f[$i]=$(i+1) works? It creates an array and appends the values based on the separator :?

                  – ajk
                  Mar 28 at 17:49











                • When i is 1 i+1 is 2 so when i is 1 $i is var1 and $(i+1) is valueA. So you get an array populated as f["var1"] = "valueA", etc. as you go through the loop.

                  – Ed Morton
                  Mar 28 at 17:52








                1




                1





                Worked great! Can you explain how f[$i]=$(i+1) works? It creates an array and appends the values based on the separator :?

                – ajk
                Mar 28 at 17:49





                Worked great! Can you explain how f[$i]=$(i+1) works? It creates an array and appends the values based on the separator :?

                – ajk
                Mar 28 at 17:49













                When i is 1 i+1 is 2 so when i is 1 $i is var1 and $(i+1) is valueA. So you get an array populated as f["var1"] = "valueA", etc. as you go through the loop.

                – Ed Morton
                Mar 28 at 17:52






                When i is 1 i+1 is 2 so when i is 1 $i is var1 and $(i+1) is valueA. So you get an array populated as f["var1"] = "valueA", etc. as you go through the loop.

                – Ed Morton
                Mar 28 at 17:52














                1
















                This is not a better answer, but it's fun:



                grep -oP 'w+:s+(.*?)(?=s+w+:|$)' file |
                sort --stable -t: -k1,1 |
                cut -d" " -f2- |
                pr -2T -s"|"





                share|improve this answer































                  1
















                  This is not a better answer, but it's fun:



                  grep -oP 'w+:s+(.*?)(?=s+w+:|$)' file |
                  sort --stable -t: -k1,1 |
                  cut -d" " -f2- |
                  pr -2T -s"|"





                  share|improve this answer





























                    1














                    1










                    1









                    This is not a better answer, but it's fun:



                    grep -oP 'w+:s+(.*?)(?=s+w+:|$)' file |
                    sort --stable -t: -k1,1 |
                    cut -d" " -f2- |
                    pr -2T -s"|"





                    share|improve this answer















                    This is not a better answer, but it's fun:



                    grep -oP 'w+:s+(.*?)(?=s+w+:|$)' file |
                    sort --stable -t: -k1,1 |
                    cut -d" " -f2- |
                    pr -2T -s"|"






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 28 at 21:25

























                    answered Mar 28 at 18:16









                    glenn jackmanglenn jackman

                    179k28 gold badges158 silver badges257 bronze badges




                    179k28 gold badges158 silver badges257 bronze badges
























                        0
















                        could you please try following.



                        awk '
                        match($0,/var1: [^ ]*/)
                        val=substr($0,RSTART+6,RLENGTH-6)
                        match($0,/var2: [^ ]*/)
                        print val"' Input_file


                        OR(since first code will have a limitation it will look for val1 string should be present as well as string val2 too, so to avoid that one could try):



                        awk '
                        match($0,/var1: [^ ]*/)
                        val=substr($0,RSTART+6,RLENGTH-6)

                        match($0,/var2: [^ ]*/)
                        print val"' Input_file


                        Output will be as follows.



                        valueA|valueB
                        valueC|valueD
                        valueE|valueF
                        valueG|valueH





                        share|improve this answer































                          0
















                          could you please try following.



                          awk '
                          match($0,/var1: [^ ]*/)
                          val=substr($0,RSTART+6,RLENGTH-6)
                          match($0,/var2: [^ ]*/)
                          print val"' Input_file


                          OR(since first code will have a limitation it will look for val1 string should be present as well as string val2 too, so to avoid that one could try):



                          awk '
                          match($0,/var1: [^ ]*/)
                          val=substr($0,RSTART+6,RLENGTH-6)

                          match($0,/var2: [^ ]*/)
                          print val"' Input_file


                          Output will be as follows.



                          valueA|valueB
                          valueC|valueD
                          valueE|valueF
                          valueG|valueH





                          share|improve this answer





























                            0














                            0










                            0









                            could you please try following.



                            awk '
                            match($0,/var1: [^ ]*/)
                            val=substr($0,RSTART+6,RLENGTH-6)
                            match($0,/var2: [^ ]*/)
                            print val"' Input_file


                            OR(since first code will have a limitation it will look for val1 string should be present as well as string val2 too, so to avoid that one could try):



                            awk '
                            match($0,/var1: [^ ]*/)
                            val=substr($0,RSTART+6,RLENGTH-6)

                            match($0,/var2: [^ ]*/)
                            print val"' Input_file


                            Output will be as follows.



                            valueA|valueB
                            valueC|valueD
                            valueE|valueF
                            valueG|valueH





                            share|improve this answer















                            could you please try following.



                            awk '
                            match($0,/var1: [^ ]*/)
                            val=substr($0,RSTART+6,RLENGTH-6)
                            match($0,/var2: [^ ]*/)
                            print val"' Input_file


                            OR(since first code will have a limitation it will look for val1 string should be present as well as string val2 too, so to avoid that one could try):



                            awk '
                            match($0,/var1: [^ ]*/)
                            val=substr($0,RSTART+6,RLENGTH-6)

                            match($0,/var2: [^ ]*/)
                            print val"' Input_file


                            Output will be as follows.



                            valueA|valueB
                            valueC|valueD
                            valueE|valueF
                            valueG|valueH






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 28 at 17:50

























                            answered Mar 28 at 17:44









                            RavinderSingh13RavinderSingh13

                            34.6k4 gold badges16 silver badges39 bronze badges




                            34.6k4 gold badges16 silver badges39 bronze badges
























                                0
















                                Using Perl



                                perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv) 
                                " print "";$s="" ' file


                                with your given inputs



                                $ cat ajk.txt
                                var1: valueA var2: valueB
                                var1: valueC var2: valueD
                                var2: valueF var1: valueE
                                var1: valueG var2: valueH

                                $ perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv)
                                " print "";$s="" ' ajk.txt
                                valueA|valueB
                                valueC|valueD
                                valueE|valueF
                                valueG|valueH

                                $





                                share|improve this answer





























                                  0
















                                  Using Perl



                                  perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv) 
                                  " print "";$s="" ' file


                                  with your given inputs



                                  $ cat ajk.txt
                                  var1: valueA var2: valueB
                                  var1: valueC var2: valueD
                                  var2: valueF var1: valueE
                                  var1: valueG var2: valueH

                                  $ perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv)
                                  " print "";$s="" ' ajk.txt
                                  valueA|valueB
                                  valueC|valueD
                                  valueE|valueF
                                  valueG|valueH

                                  $





                                  share|improve this answer



























                                    0














                                    0










                                    0









                                    Using Perl



                                    perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv) 
                                    " print "";$s="" ' file


                                    with your given inputs



                                    $ cat ajk.txt
                                    var1: valueA var2: valueB
                                    var1: valueC var2: valueD
                                    var2: valueF var1: valueE
                                    var1: valueG var2: valueH

                                    $ perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv)
                                    " print "";$s="" ' ajk.txt
                                    valueA|valueB
                                    valueC|valueD
                                    valueE|valueF
                                    valueG|valueH

                                    $





                                    share|improve this answer













                                    Using Perl



                                    perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv) 
                                    " print "";$s="" ' file


                                    with your given inputs



                                    $ cat ajk.txt
                                    var1: valueA var2: valueB
                                    var1: valueC var2: valueD
                                    var2: valueF var1: valueE
                                    var1: valueG var2: valueH

                                    $ perl -lne ' %kv=/(varS+)s+(S+)/g; for(sort keys %kv)
                                    " print "";$s="" ' ajk.txt
                                    valueA|valueB
                                    valueC|valueD
                                    valueE|valueF
                                    valueG|valueH

                                    $






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 28 at 19:19









                                    stack0114106stack0114106

                                    5,4262 gold badges4 silver badges25 bronze badges




                                    5,4262 gold badges4 silver badges25 bronze badges































                                        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%2f55403827%2fsetting-multiples-out-of-order-variables-with-pattern%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

                                        Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                        Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                        Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript