Shell script: split line

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











up vote
3
down vote

favorite












I need to split a line with two string separated by an space like: key value.



I've tried:



key=$(awk -FS=" " print $1 line)
value=$(awk -FS=" " print $2 line)


But I'm getting:




awk: line 2: missing } near end of file




Any ideas?







share|improve this question























    up vote
    3
    down vote

    favorite












    I need to split a line with two string separated by an space like: key value.



    I've tried:



    key=$(awk -FS=" " print $1 line)
    value=$(awk -FS=" " print $2 line)


    But I'm getting:




    awk: line 2: missing } near end of file




    Any ideas?







    share|improve this question





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I need to split a line with two string separated by an space like: key value.



      I've tried:



      key=$(awk -FS=" " print $1 line)
      value=$(awk -FS=" " print $2 line)


      But I'm getting:




      awk: line 2: missing } near end of file




      Any ideas?







      share|improve this question











      I need to split a line with two string separated by an space like: key value.



      I've tried:



      key=$(awk -FS=" " print $1 line)
      value=$(awk -FS=" " print $2 line)


      But I'm getting:




      awk: line 2: missing } near end of file




      Any ideas?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jun 28 at 7:09









      Jordi

      1424




      1424




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          6
          down vote













          An awk script on the command line should be single quoted:



          awk -F ' ' ' print $1 ' filename


          Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....



          Your command, the way you have written it, also assumes that line is a file name.




          If $line is a string with a single space in it, you can separate it into two strings with



          first_part=$line% * # removes the space and everything after it
          second_part=$line#* # removes the space and everything before it


          Likewise, if $line is a string with a = in it:



          first_part=$line%=* # removes the = and everything after it
          second_part=$line#*= # removes the = and everything before it





          share|improve this answer






























            up vote
            3
            down vote













            You can also use an array for splitting a line on spaces:



            if line is a string



             arr=($line)
            key="$arr[0]"
            value="$arr[1]"



            Note:- If the first word of $line is * then the arr array will contain
            all the filenames in the current directory. So to be on the safe side and avoid such situations , use



            set -f; arr=($line); set +f
            key="$arr[0]"
            value="$arr[1]"




            If line is file



            while read -r words
            do
            set -- $words
            key=$1
            value=$2
            done < line





            share|improve this answer























            • line in the question is a filename.
              – Kusalananda
              Jun 28 at 7:24










            • @Kusalananda i thought it was a string, edited my answer for both the situations
              – Arushix
              Jun 28 at 7:30










            • In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is * then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +f
              – glenn jackman
              Jun 28 at 11:30










            • @Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
              – glenn jackman
              Jun 28 at 11:37











            • @glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
              – Arushix
              Jun 28 at 12:13

















            up vote
            1
            down vote













            You can easily achieve this, without using awk, that is inteded for more complex data manipulation.
            cut bash command is all you need.



            key="$(echo "$line" | cut -d ' ' -f 1)"
            value="$(echo "$line" | cut -d ' ' -f 2)"


            Hope it helped.






            share|improve this answer



















            • 1




              Quote your variables
              – glenn jackman
              Jun 28 at 11:32

















            up vote
            0
            down vote













            You can use the shell's parameter expansion facilities instead of calling out to an external program:



            key=$line%% * # from the end remove the longest string matching
            # a space followed by any characters

            value=$line#* # from the start remove the shortest string matching
            # any characters followed by a space





            share|improve this answer




























              up vote
              0
              down vote













              read



              Just use read:



              read key value



              Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.






              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
                );



                );








                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452363%2fshell-script-split-line%23new-answer', 'question_page');

                );

                Post as a guest






























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                6
                down vote













                An awk script on the command line should be single quoted:



                awk -F ' ' ' print $1 ' filename


                Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....



                Your command, the way you have written it, also assumes that line is a file name.




                If $line is a string with a single space in it, you can separate it into two strings with



                first_part=$line% * # removes the space and everything after it
                second_part=$line#* # removes the space and everything before it


                Likewise, if $line is a string with a = in it:



                first_part=$line%=* # removes the = and everything after it
                second_part=$line#*= # removes the = and everything before it





                share|improve this answer



























                  up vote
                  6
                  down vote













                  An awk script on the command line should be single quoted:



                  awk -F ' ' ' print $1 ' filename


                  Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....



                  Your command, the way you have written it, also assumes that line is a file name.




                  If $line is a string with a single space in it, you can separate it into two strings with



                  first_part=$line% * # removes the space and everything after it
                  second_part=$line#* # removes the space and everything before it


                  Likewise, if $line is a string with a = in it:



                  first_part=$line%=* # removes the = and everything after it
                  second_part=$line#*= # removes the = and everything before it





                  share|improve this answer

























                    up vote
                    6
                    down vote










                    up vote
                    6
                    down vote









                    An awk script on the command line should be single quoted:



                    awk -F ' ' ' print $1 ' filename


                    Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....



                    Your command, the way you have written it, also assumes that line is a file name.




                    If $line is a string with a single space in it, you can separate it into two strings with



                    first_part=$line% * # removes the space and everything after it
                    second_part=$line#* # removes the space and everything before it


                    Likewise, if $line is a string with a = in it:



                    first_part=$line%=* # removes the = and everything after it
                    second_part=$line#*= # removes the = and everything before it





                    share|improve this answer















                    An awk script on the command line should be single quoted:



                    awk -F ' ' ' print $1 ' filename


                    Notice the single quotes around ... , and also that the correct way to set FS is through -F, or through -v FS=... or in a BEGIN block, but not with -FS=....



                    Your command, the way you have written it, also assumes that line is a file name.




                    If $line is a string with a single space in it, you can separate it into two strings with



                    first_part=$line% * # removes the space and everything after it
                    second_part=$line#* # removes the space and everything before it


                    Likewise, if $line is a string with a = in it:



                    first_part=$line%=* # removes the = and everything after it
                    second_part=$line#*= # removes the = and everything before it






                    share|improve this answer















                    share|improve this answer



                    share|improve this answer








                    edited Jun 28 at 13:34


























                    answered Jun 28 at 7:12









                    Kusalananda

                    101k13199312




                    101k13199312






















                        up vote
                        3
                        down vote













                        You can also use an array for splitting a line on spaces:



                        if line is a string



                         arr=($line)
                        key="$arr[0]"
                        value="$arr[1]"



                        Note:- If the first word of $line is * then the arr array will contain
                        all the filenames in the current directory. So to be on the safe side and avoid such situations , use



                        set -f; arr=($line); set +f
                        key="$arr[0]"
                        value="$arr[1]"




                        If line is file



                        while read -r words
                        do
                        set -- $words
                        key=$1
                        value=$2
                        done < line





                        share|improve this answer























                        • line in the question is a filename.
                          – Kusalananda
                          Jun 28 at 7:24










                        • @Kusalananda i thought it was a string, edited my answer for both the situations
                          – Arushix
                          Jun 28 at 7:30










                        • In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is * then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +f
                          – glenn jackman
                          Jun 28 at 11:30










                        • @Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
                          – glenn jackman
                          Jun 28 at 11:37











                        • @glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
                          – Arushix
                          Jun 28 at 12:13














                        up vote
                        3
                        down vote













                        You can also use an array for splitting a line on spaces:



                        if line is a string



                         arr=($line)
                        key="$arr[0]"
                        value="$arr[1]"



                        Note:- If the first word of $line is * then the arr array will contain
                        all the filenames in the current directory. So to be on the safe side and avoid such situations , use



                        set -f; arr=($line); set +f
                        key="$arr[0]"
                        value="$arr[1]"




                        If line is file



                        while read -r words
                        do
                        set -- $words
                        key=$1
                        value=$2
                        done < line





                        share|improve this answer























                        • line in the question is a filename.
                          – Kusalananda
                          Jun 28 at 7:24










                        • @Kusalananda i thought it was a string, edited my answer for both the situations
                          – Arushix
                          Jun 28 at 7:30










                        • In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is * then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +f
                          – glenn jackman
                          Jun 28 at 11:30










                        • @Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
                          – glenn jackman
                          Jun 28 at 11:37











                        • @glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
                          – Arushix
                          Jun 28 at 12:13












                        up vote
                        3
                        down vote










                        up vote
                        3
                        down vote









                        You can also use an array for splitting a line on spaces:



                        if line is a string



                         arr=($line)
                        key="$arr[0]"
                        value="$arr[1]"



                        Note:- If the first word of $line is * then the arr array will contain
                        all the filenames in the current directory. So to be on the safe side and avoid such situations , use



                        set -f; arr=($line); set +f
                        key="$arr[0]"
                        value="$arr[1]"




                        If line is file



                        while read -r words
                        do
                        set -- $words
                        key=$1
                        value=$2
                        done < line





                        share|improve this answer















                        You can also use an array for splitting a line on spaces:



                        if line is a string



                         arr=($line)
                        key="$arr[0]"
                        value="$arr[1]"



                        Note:- If the first word of $line is * then the arr array will contain
                        all the filenames in the current directory. So to be on the safe side and avoid such situations , use



                        set -f; arr=($line); set +f
                        key="$arr[0]"
                        value="$arr[1]"




                        If line is file



                        while read -r words
                        do
                        set -- $words
                        key=$1
                        value=$2
                        done < line






                        share|improve this answer















                        share|improve this answer



                        share|improve this answer








                        edited Jun 28 at 12:12


























                        answered Jun 28 at 7:22









                        Arushix

                        9968




                        9968











                        • line in the question is a filename.
                          – Kusalananda
                          Jun 28 at 7:24










                        • @Kusalananda i thought it was a string, edited my answer for both the situations
                          – Arushix
                          Jun 28 at 7:30










                        • In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is * then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +f
                          – glenn jackman
                          Jun 28 at 11:30










                        • @Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
                          – glenn jackman
                          Jun 28 at 11:37











                        • @glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
                          – Arushix
                          Jun 28 at 12:13
















                        • line in the question is a filename.
                          – Kusalananda
                          Jun 28 at 7:24










                        • @Kusalananda i thought it was a string, edited my answer for both the situations
                          – Arushix
                          Jun 28 at 7:30










                        • In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is * then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +f
                          – glenn jackman
                          Jun 28 at 11:30










                        • @Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
                          – glenn jackman
                          Jun 28 at 11:37











                        • @glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
                          – Arushix
                          Jun 28 at 12:13















                        line in the question is a filename.
                        – Kusalananda
                        Jun 28 at 7:24




                        line in the question is a filename.
                        – Kusalananda
                        Jun 28 at 7:24












                        @Kusalananda i thought it was a string, edited my answer for both the situations
                        – Arushix
                        Jun 28 at 7:30




                        @Kusalananda i thought it was a string, edited my answer for both the situations
                        – Arushix
                        Jun 28 at 7:30












                        In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is * then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +f
                        – glenn jackman
                        Jun 28 at 11:30




                        In the first snippet, you are leaving the variable unquoted to employ word splitting. However you are also subject to filename expansion. If the first word of $line is * then the arr array will contain all the filenames in the current directory. You will have to set -f; arr=($line); set +f
                        – glenn jackman
                        Jun 28 at 11:30












                        @Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
                        – glenn jackman
                        Jun 28 at 11:37





                        @Kusalananda, I wouldn't assume that it's a file, given the apparent experience level of the question. I would put more trust in the words: "I need to split a line..."
                        – glenn jackman
                        Jun 28 at 11:37













                        @glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
                        – Arushix
                        Jun 28 at 12:13




                        @glennjackman thanks for the suggestion, i did not considered that situation, added in my answer too :)
                        – Arushix
                        Jun 28 at 12:13










                        up vote
                        1
                        down vote













                        You can easily achieve this, without using awk, that is inteded for more complex data manipulation.
                        cut bash command is all you need.



                        key="$(echo "$line" | cut -d ' ' -f 1)"
                        value="$(echo "$line" | cut -d ' ' -f 2)"


                        Hope it helped.






                        share|improve this answer



















                        • 1




                          Quote your variables
                          – glenn jackman
                          Jun 28 at 11:32














                        up vote
                        1
                        down vote













                        You can easily achieve this, without using awk, that is inteded for more complex data manipulation.
                        cut bash command is all you need.



                        key="$(echo "$line" | cut -d ' ' -f 1)"
                        value="$(echo "$line" | cut -d ' ' -f 2)"


                        Hope it helped.






                        share|improve this answer



















                        • 1




                          Quote your variables
                          – glenn jackman
                          Jun 28 at 11:32












                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        You can easily achieve this, without using awk, that is inteded for more complex data manipulation.
                        cut bash command is all you need.



                        key="$(echo "$line" | cut -d ' ' -f 1)"
                        value="$(echo "$line" | cut -d ' ' -f 2)"


                        Hope it helped.






                        share|improve this answer















                        You can easily achieve this, without using awk, that is inteded for more complex data manipulation.
                        cut bash command is all you need.



                        key="$(echo "$line" | cut -d ' ' -f 1)"
                        value="$(echo "$line" | cut -d ' ' -f 2)"


                        Hope it helped.







                        share|improve this answer















                        share|improve this answer



                        share|improve this answer








                        edited Jun 28 at 13:42









                        ilkkachu

                        47.3k668130




                        47.3k668130











                        answered Jun 28 at 10:15









                        Gio G.

                        313




                        313







                        • 1




                          Quote your variables
                          – glenn jackman
                          Jun 28 at 11:32












                        • 1




                          Quote your variables
                          – glenn jackman
                          Jun 28 at 11:32







                        1




                        1




                        Quote your variables
                        – glenn jackman
                        Jun 28 at 11:32




                        Quote your variables
                        – glenn jackman
                        Jun 28 at 11:32










                        up vote
                        0
                        down vote













                        You can use the shell's parameter expansion facilities instead of calling out to an external program:



                        key=$line%% * # from the end remove the longest string matching
                        # a space followed by any characters

                        value=$line#* # from the start remove the shortest string matching
                        # any characters followed by a space





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          You can use the shell's parameter expansion facilities instead of calling out to an external program:



                          key=$line%% * # from the end remove the longest string matching
                          # a space followed by any characters

                          value=$line#* # from the start remove the shortest string matching
                          # any characters followed by a space





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You can use the shell's parameter expansion facilities instead of calling out to an external program:



                            key=$line%% * # from the end remove the longest string matching
                            # a space followed by any characters

                            value=$line#* # from the start remove the shortest string matching
                            # any characters followed by a space





                            share|improve this answer













                            You can use the shell's parameter expansion facilities instead of calling out to an external program:



                            key=$line%% * # from the end remove the longest string matching
                            # a space followed by any characters

                            value=$line#* # from the start remove the shortest string matching
                            # any characters followed by a space






                            share|improve this answer













                            share|improve this answer



                            share|improve this answer











                            answered Jun 28 at 11:36









                            glenn jackman

                            45.6k264100




                            45.6k264100




















                                up vote
                                0
                                down vote













                                read



                                Just use read:



                                read key value



                                Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.






                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  read



                                  Just use read:



                                  read key value



                                  Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.






                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    read



                                    Just use read:



                                    read key value



                                    Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.






                                    share|improve this answer













                                    read



                                    Just use read:



                                    read key value



                                    Everything before the first space on the line goes into key and everything after it (including any additional spaces) goes into value.







                                    share|improve this answer













                                    share|improve this answer



                                    share|improve this answer











                                    answered Jun 28 at 13:49









                                    Monty Harder

                                    21815




                                    21815






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452363%2fshell-script-split-line%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        Peggy Mitchell

                                        Palaiologos

                                        The Forum (Inglewood, California)