How to get single character after space?Using sed command convert only first character in specific line with uppercaseHow to convert fixed length fields in a file to space delimitedInsert space at specified position in a fileHow to change all lines consisting of one “0” character to something else?Deleting lines by matching 3rd and 4th character onlyFail to grep by first charactermodify specific column with sed or awkhow to remove double strings with sed grep awkUsing cut and sed command at the same time in a scriptsed: insert text after specific character in a specific line

What does it mean by "d-ism of Leibniz" and "dotage of Newton" in simple English?

Did airlines fly their aircraft slower in response to oil prices in the 1970s?

What caused the tendency for conservatives to not support climate change regulations?

Looking after a wayward brother in mother's will

How to detach yourself from a character you're going to kill?

Starting VLC from command line always puts the window behind other windows

Are grass strips more dangerous than tarmac?

Is the capacitor drawn or wired wrongly?

Are there mythical creatures in the world of Game of Thrones?

Is having a hidden directory under /etc safe?

Accidentally cashed a check twice

Double integral bounds of integration polar change of coordinate

When was the word "ambigu" first used with the sense of "meal with all items served at the same time"?

Can you please explain this joke: "I'm going bananas is what I tell my bananas before I leave the house"?

Explain Ant-Man's "not it" scene from Avengers: Endgame

Question about IV chord in minor key

Applicants clearly not having the skills they advertise

Opposite of "Squeaky wheel gets the grease"

What if you don't bring your credit card or debit for incidentals?

Why don't I have ground wiring on any of my outlets?

Joist hangers to use for rough cut 2x8 (2 3/4" x 8 3/4")?

Where can I find the list of all tendons in the human body?

Why would Lupin kill Pettigrew?

What is the difference between a game ban and a VAC ban in Steam?



How to get single character after space?


Using sed command convert only first character in specific line with uppercaseHow to convert fixed length fields in a file to space delimitedInsert space at specified position in a fileHow to change all lines consisting of one “0” character to something else?Deleting lines by matching 3rd and 4th character onlyFail to grep by first charactermodify specific column with sed or awkhow to remove double strings with sed grep awkUsing cut and sed command at the same time in a scriptsed: insert text after specific character in a specific line






.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 Apr 14 at 4:45









      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нιη
            Apr 14 at 6:41












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

            – Kusalananda
            Apr 14 at 6:43


















          6














          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
            Apr 14 at 12:03


















          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
            Apr 15 at 5:05











          • It's not needed at all. Merely to show the contents of the array, to see the results of the regex match with capturing parentheses.

            – glenn jackman
            Apr 15 at 10:42












          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нιη
            Apr 14 at 6:41












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

            – Kusalananda
            Apr 14 at 6:43















          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нιη
            Apr 14 at 6:41












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

            – Kusalananda
            Apr 14 at 6:43













          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 Apr 14 at 6:37

























          answered Apr 14 at 6:29









          KusalanandaKusalananda

          148k18279468




          148k18279468












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

            – αғsнιη
            Apr 14 at 6:41












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

            – Kusalananda
            Apr 14 at 6:43

















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

            – αғsнιη
            Apr 14 at 6:41












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

            – Kusalananda
            Apr 14 at 6:43
















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

          – αғsнιη
          Apr 14 at 6:41






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

          – αғsнιη
          Apr 14 at 6:41














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

          – Kusalananda
          Apr 14 at 6:43





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

          – Kusalananda
          Apr 14 at 6:43













          6














          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
            Apr 14 at 12:03















          6














          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
            Apr 14 at 12:03













          6












          6








          6







          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 Apr 14 at 12:28

























          answered Apr 14 at 5:31









          FreddyFreddy

          3,8231417




          3,8231417












          • 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
            Apr 14 at 12:03

















          • 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
            Apr 14 at 12:03
















          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
          Apr 14 at 12:03





          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
          Apr 14 at 12:03











          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
            Apr 15 at 5:05











          • It's not needed at all. Merely to show the contents of the array, to see the results of the regex match with capturing parentheses.

            – glenn jackman
            Apr 15 at 10:42
















          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
            Apr 15 at 5:05











          • It's not needed at all. Merely to show the contents of the array, to see the results of the regex match with capturing parentheses.

            – glenn jackman
            Apr 15 at 10:42














          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 Apr 14 at 12:05









          glenn jackmanglenn jackman

          53.9k674115




          53.9k674115












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

            – Inian
            Apr 15 at 5:05











          • It's not needed at all. Merely to show the contents of the array, to see the results of the regex match with capturing parentheses.

            – glenn jackman
            Apr 15 at 10:42


















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

            – Inian
            Apr 15 at 5:05











          • It's not needed at all. Merely to show the contents of the array, to see the results of the regex match with capturing parentheses.

            – glenn jackman
            Apr 15 at 10:42

















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

          – Inian
          Apr 15 at 5:05





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

          – Inian
          Apr 15 at 5:05













          It's not needed at all. Merely to show the contents of the array, to see the results of the regex match with capturing parentheses.

          – glenn jackman
          Apr 15 at 10:42






          It's not needed at all. Merely to show the contents of the array, to see the results of the regex match with capturing parentheses.

          – glenn jackman
          Apr 15 at 10:42


















          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

          Distance measures on a map of a game The 2019 Stack Overflow Developer Survey Results Are Inmin distance in a graphShortest distance path on contour plotHow to plot a tilted map?Finding points outside of a diskDelaunay link distanceAnnulus from GeoDisks: drawing a ring on a mapNegative Correlation DistanceFind distance along a path (GPS coordinates)Finding position at given distance in a GeoPathMathematics behind distance estimation using camera

          How to get a smooth, uniform ParametricPlot of a 2D Region?How to plot a complicated Region?How to exclude a region from ParametricPlotHow discretize a region placing vertices on a specific non-uniform gridHow to transform a Plot or a ParametricPlot into a RegionHow can I get a smooth plot of a bounded region?Smooth ParametricPlot3D with RegionFunction?Smooth border of a region ParametricPlotSmooth region boundarySmooth region plot from list of pointsGet minimum y of a certain x in a region

          Genealogie vun de Merowenger Vum Merowech bis zum Chilperich I. | Navigatiounsmenü