How to get single character after space?





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







2















How can I obtain below output? I want the first field as it is and a single character after space.



echo "Hello world"
Hellow


If it also has a 3rd field than the beginning character of the 3rd field should be in the output.



echo "hello world unix"
hellou









share|improve this question





























    2















    How can I obtain below output? I want the first field as it is and a single character after space.



    echo "Hello world"
    Hellow


    If it also has a 3rd field than the beginning character of the 3rd field should be in the output.



    echo "hello world unix"
    hellou









    share|improve this question

























      2












      2








      2


      0






      How can I obtain below output? I want the first field as it is and a single character after space.



      echo "Hello world"
      Hellow


      If it also has a 3rd field than the beginning character of the 3rd field should be in the output.



      echo "hello world unix"
      hellou









      share|improve this question














      How can I obtain below output? I want the first field as it is and a single character after space.



      echo "Hello world"
      Hellow


      If it also has a 3rd field than the beginning character of the 3rd field should be in the output.



      echo "hello world unix"
      hellou






      linux awk sed grep cut






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      BDNBDN

      130112




      130112






















          3 Answers
          3






          active

          oldest

          votes


















          4














          Using awk to output the first whitespace-delimited word concatenated with the first character of the last whitespace-delimited word:



          awk '{ print $1 substr($NF, 1, 1) }'


          The substr() function returns a number of characters from a given position of a string, and $1 and $NF is the first and last whitespace-delimited word on the current line, respectively.



          Testing:



          $ echo 'hello world' | awk '{ print $1 substr($NF, 1, 1) }'
          hellow




          $ echo 'apple beet carrot' | awk '{ print $1 substr($NF, 1, 1) }'
          applec





          share|improve this answer


























          • Will not this duplicated the first letter of a word for a line with that single word only?

            – αғsнιη
            22 hours ago













          • @αғsнιη Yes it would. The question does not specify what should happen in the case when there is only a single word.

            – Kusalananda
            22 hours ago



















          5














          With sed:



          Edit: improved by glenn jackmann, thanks!



          $ echo "Hello world" | sed -E 's/(S+).*s(S).*$/12/'
          Hellow
          $ echo "hello world unix" | sed -E 's/(S+).*s(S).*$/12/'
          hellou


          Description with "hello world unix" as example:





          • s/ substitute the following pattern


          • (S+) 1st group, one or more non-space characters: "hello"


          • .* the middle part, any characters: " world"


          • s space character: " "


          • (S) 2nd group, non-space character: "u"


          • .*$ any characters up to the end: "nix"


          • /12/ replace with 1st and 2nd group: "hellou"


          With bash:



          $ var="Hello world"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          Hellow

          $ var="hello world unix"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          hellou


          Description with "hello world unix" as example:





          • var_end=${var##* } remove matching prefix pattern, longest match,

            "hello world ", result: "unix"


          • ${var%% *} remove matching suffix pattern, longest match,

            " world unix", result: "hello"


          • ${var_end:0:1} get the first character: "u"






          share|improve this answer


























          • Your regex can be a bit simpler: you don't need to capture the 2nd group, and it can contain any characters: ^(S+).*s(S) will do. Also, I believe the perl-like regex means you must use GNU sed.

            – glenn jackman
            17 hours ago











          • @glennjackman Edited, thank you!

            – Freddy
            16 hours ago



















          3














          Using bash:



          text="hello world unix"
          if [[ $text =~ ^([^[:space:]]+).*[[:space:]]([^[:space:]]) ]]; then
          declare -p BASH_REMATCH
          echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
          fi




          declare -ar BASH_REMATCH='([0]="hello world u" [1]="hello" [2]="u")'
          hellou





          share|improve this answer
























          • just curious to understand to know, why is declare -p needed here? The array is already populated right?

            – Inian
            10 mins ago












          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%2f512357%2fhow-to-get-single-character-after-space%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Using awk to output the first whitespace-delimited word concatenated with the first character of the last whitespace-delimited word:



          awk '{ print $1 substr($NF, 1, 1) }'


          The substr() function returns a number of characters from a given position of a string, and $1 and $NF is the first and last whitespace-delimited word on the current line, respectively.



          Testing:



          $ echo 'hello world' | awk '{ print $1 substr($NF, 1, 1) }'
          hellow




          $ echo 'apple beet carrot' | awk '{ print $1 substr($NF, 1, 1) }'
          applec





          share|improve this answer


























          • Will not this duplicated the first letter of a word for a line with that single word only?

            – αғsнιη
            22 hours ago













          • @αғsнιη Yes it would. The question does not specify what should happen in the case when there is only a single word.

            – Kusalananda
            22 hours ago
















          4














          Using awk to output the first whitespace-delimited word concatenated with the first character of the last whitespace-delimited word:



          awk '{ print $1 substr($NF, 1, 1) }'


          The substr() function returns a number of characters from a given position of a string, and $1 and $NF is the first and last whitespace-delimited word on the current line, respectively.



          Testing:



          $ echo 'hello world' | awk '{ print $1 substr($NF, 1, 1) }'
          hellow




          $ echo 'apple beet carrot' | awk '{ print $1 substr($NF, 1, 1) }'
          applec





          share|improve this answer


























          • Will not this duplicated the first letter of a word for a line with that single word only?

            – αғsнιη
            22 hours ago













          • @αғsнιη Yes it would. The question does not specify what should happen in the case when there is only a single word.

            – Kusalananda
            22 hours ago














          4












          4








          4







          Using awk to output the first whitespace-delimited word concatenated with the first character of the last whitespace-delimited word:



          awk '{ print $1 substr($NF, 1, 1) }'


          The substr() function returns a number of characters from a given position of a string, and $1 and $NF is the first and last whitespace-delimited word on the current line, respectively.



          Testing:



          $ echo 'hello world' | awk '{ print $1 substr($NF, 1, 1) }'
          hellow




          $ echo 'apple beet carrot' | awk '{ print $1 substr($NF, 1, 1) }'
          applec





          share|improve this answer















          Using awk to output the first whitespace-delimited word concatenated with the first character of the last whitespace-delimited word:



          awk '{ print $1 substr($NF, 1, 1) }'


          The substr() function returns a number of characters from a given position of a string, and $1 and $NF is the first and last whitespace-delimited word on the current line, respectively.



          Testing:



          $ echo 'hello world' | awk '{ print $1 substr($NF, 1, 1) }'
          hellow




          $ echo 'apple beet carrot' | awk '{ print $1 substr($NF, 1, 1) }'
          applec






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 22 hours ago

























          answered 22 hours ago









          KusalanandaKusalananda

          141k18264440




          141k18264440













          • Will not this duplicated the first letter of a word for a line with that single word only?

            – αғsнιη
            22 hours ago













          • @αғsнιη Yes it would. The question does not specify what should happen in the case when there is only a single word.

            – Kusalananda
            22 hours ago



















          • Will not this duplicated the first letter of a word for a line with that single word only?

            – αғsнιη
            22 hours ago













          • @αғsнιη Yes it would. The question does not specify what should happen in the case when there is only a single word.

            – Kusalananda
            22 hours ago

















          Will not this duplicated the first letter of a word for a line with that single word only?

          – αғsнιη
          22 hours ago







          Will not this duplicated the first letter of a word for a line with that single word only?

          – αғsнιη
          22 hours ago















          @αғsнιη Yes it would. The question does not specify what should happen in the case when there is only a single word.

          – Kusalananda
          22 hours ago





          @αғsнιη Yes it would. The question does not specify what should happen in the case when there is only a single word.

          – Kusalananda
          22 hours ago













          5














          With sed:



          Edit: improved by glenn jackmann, thanks!



          $ echo "Hello world" | sed -E 's/(S+).*s(S).*$/12/'
          Hellow
          $ echo "hello world unix" | sed -E 's/(S+).*s(S).*$/12/'
          hellou


          Description with "hello world unix" as example:





          • s/ substitute the following pattern


          • (S+) 1st group, one or more non-space characters: "hello"


          • .* the middle part, any characters: " world"


          • s space character: " "


          • (S) 2nd group, non-space character: "u"


          • .*$ any characters up to the end: "nix"


          • /12/ replace with 1st and 2nd group: "hellou"


          With bash:



          $ var="Hello world"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          Hellow

          $ var="hello world unix"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          hellou


          Description with "hello world unix" as example:





          • var_end=${var##* } remove matching prefix pattern, longest match,

            "hello world ", result: "unix"


          • ${var%% *} remove matching suffix pattern, longest match,

            " world unix", result: "hello"


          • ${var_end:0:1} get the first character: "u"






          share|improve this answer


























          • Your regex can be a bit simpler: you don't need to capture the 2nd group, and it can contain any characters: ^(S+).*s(S) will do. Also, I believe the perl-like regex means you must use GNU sed.

            – glenn jackman
            17 hours ago











          • @glennjackman Edited, thank you!

            – Freddy
            16 hours ago
















          5














          With sed:



          Edit: improved by glenn jackmann, thanks!



          $ echo "Hello world" | sed -E 's/(S+).*s(S).*$/12/'
          Hellow
          $ echo "hello world unix" | sed -E 's/(S+).*s(S).*$/12/'
          hellou


          Description with "hello world unix" as example:





          • s/ substitute the following pattern


          • (S+) 1st group, one or more non-space characters: "hello"


          • .* the middle part, any characters: " world"


          • s space character: " "


          • (S) 2nd group, non-space character: "u"


          • .*$ any characters up to the end: "nix"


          • /12/ replace with 1st and 2nd group: "hellou"


          With bash:



          $ var="Hello world"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          Hellow

          $ var="hello world unix"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          hellou


          Description with "hello world unix" as example:





          • var_end=${var##* } remove matching prefix pattern, longest match,

            "hello world ", result: "unix"


          • ${var%% *} remove matching suffix pattern, longest match,

            " world unix", result: "hello"


          • ${var_end:0:1} get the first character: "u"






          share|improve this answer


























          • Your regex can be a bit simpler: you don't need to capture the 2nd group, and it can contain any characters: ^(S+).*s(S) will do. Also, I believe the perl-like regex means you must use GNU sed.

            – glenn jackman
            17 hours ago











          • @glennjackman Edited, thank you!

            – Freddy
            16 hours ago














          5












          5








          5







          With sed:



          Edit: improved by glenn jackmann, thanks!



          $ echo "Hello world" | sed -E 's/(S+).*s(S).*$/12/'
          Hellow
          $ echo "hello world unix" | sed -E 's/(S+).*s(S).*$/12/'
          hellou


          Description with "hello world unix" as example:





          • s/ substitute the following pattern


          • (S+) 1st group, one or more non-space characters: "hello"


          • .* the middle part, any characters: " world"


          • s space character: " "


          • (S) 2nd group, non-space character: "u"


          • .*$ any characters up to the end: "nix"


          • /12/ replace with 1st and 2nd group: "hellou"


          With bash:



          $ var="Hello world"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          Hellow

          $ var="hello world unix"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          hellou


          Description with "hello world unix" as example:





          • var_end=${var##* } remove matching prefix pattern, longest match,

            "hello world ", result: "unix"


          • ${var%% *} remove matching suffix pattern, longest match,

            " world unix", result: "hello"


          • ${var_end:0:1} get the first character: "u"






          share|improve this answer















          With sed:



          Edit: improved by glenn jackmann, thanks!



          $ echo "Hello world" | sed -E 's/(S+).*s(S).*$/12/'
          Hellow
          $ echo "hello world unix" | sed -E 's/(S+).*s(S).*$/12/'
          hellou


          Description with "hello world unix" as example:





          • s/ substitute the following pattern


          • (S+) 1st group, one or more non-space characters: "hello"


          • .* the middle part, any characters: " world"


          • s space character: " "


          • (S) 2nd group, non-space character: "u"


          • .*$ any characters up to the end: "nix"


          • /12/ replace with 1st and 2nd group: "hellou"


          With bash:



          $ var="Hello world"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          Hellow

          $ var="hello world unix"
          $ var_end=${var##* };echo ${var%% *}${var_end:0:1}
          hellou


          Description with "hello world unix" as example:





          • var_end=${var##* } remove matching prefix pattern, longest match,

            "hello world ", result: "unix"


          • ${var%% *} remove matching suffix pattern, longest match,

            " world unix", result: "hello"


          • ${var_end:0:1} get the first character: "u"







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 16 hours ago

























          answered 23 hours ago









          FreddyFreddy

          1,859210




          1,859210













          • Your regex can be a bit simpler: you don't need to capture the 2nd group, and it can contain any characters: ^(S+).*s(S) will do. Also, I believe the perl-like regex means you must use GNU sed.

            – glenn jackman
            17 hours ago











          • @glennjackman Edited, thank you!

            – Freddy
            16 hours ago



















          • Your regex can be a bit simpler: you don't need to capture the 2nd group, and it can contain any characters: ^(S+).*s(S) will do. Also, I believe the perl-like regex means you must use GNU sed.

            – glenn jackman
            17 hours ago











          • @glennjackman Edited, thank you!

            – Freddy
            16 hours ago

















          Your regex can be a bit simpler: you don't need to capture the 2nd group, and it can contain any characters: ^(S+).*s(S) will do. Also, I believe the perl-like regex means you must use GNU sed.

          – glenn jackman
          17 hours ago





          Your regex can be a bit simpler: you don't need to capture the 2nd group, and it can contain any characters: ^(S+).*s(S) will do. Also, I believe the perl-like regex means you must use GNU sed.

          – glenn jackman
          17 hours ago













          @glennjackman Edited, thank you!

          – Freddy
          16 hours ago





          @glennjackman Edited, thank you!

          – Freddy
          16 hours ago











          3














          Using bash:



          text="hello world unix"
          if [[ $text =~ ^([^[:space:]]+).*[[:space:]]([^[:space:]]) ]]; then
          declare -p BASH_REMATCH
          echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
          fi




          declare -ar BASH_REMATCH='([0]="hello world u" [1]="hello" [2]="u")'
          hellou





          share|improve this answer
























          • just curious to understand to know, why is declare -p needed here? The array is already populated right?

            – Inian
            10 mins ago
















          3














          Using bash:



          text="hello world unix"
          if [[ $text =~ ^([^[:space:]]+).*[[:space:]]([^[:space:]]) ]]; then
          declare -p BASH_REMATCH
          echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
          fi




          declare -ar BASH_REMATCH='([0]="hello world u" [1]="hello" [2]="u")'
          hellou





          share|improve this answer
























          • just curious to understand to know, why is declare -p needed here? The array is already populated right?

            – Inian
            10 mins ago














          3












          3








          3







          Using bash:



          text="hello world unix"
          if [[ $text =~ ^([^[:space:]]+).*[[:space:]]([^[:space:]]) ]]; then
          declare -p BASH_REMATCH
          echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
          fi




          declare -ar BASH_REMATCH='([0]="hello world u" [1]="hello" [2]="u")'
          hellou





          share|improve this answer













          Using bash:



          text="hello world unix"
          if [[ $text =~ ^([^[:space:]]+).*[[:space:]]([^[:space:]]) ]]; then
          declare -p BASH_REMATCH
          echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
          fi




          declare -ar BASH_REMATCH='([0]="hello world u" [1]="hello" [2]="u")'
          hellou






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 17 hours ago









          glenn jackmanglenn jackman

          53.1k573114




          53.1k573114













          • just curious to understand to know, why is declare -p needed here? The array is already populated right?

            – Inian
            10 mins ago



















          • just curious to understand to know, why is declare -p needed here? The array is already populated right?

            – Inian
            10 mins ago

















          just curious to understand to know, why is declare -p needed here? The array is already populated right?

          – Inian
          10 mins ago





          just curious to understand to know, why is declare -p needed here? The array is already populated right?

          – Inian
          10 mins ago


















          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%2f512357%2fhow-to-get-single-character-after-space%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

          GameSpot

          日野市

          Tu-95轟炸機