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;
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
add a comment |
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
add a comment |
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
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
linux awk sed grep cut
asked Apr 14 at 4:45
BDNBDN
130112
130112
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
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
add a comment |
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"sspace 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:1get the first character: "u"
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
add a comment |
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
just curious to understand to know, why isdeclare -pneeded 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
edited Apr 14 at 6:37
answered Apr 14 at 6:29
Kusalananda♦Kusalananda
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
add a comment |
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
add a comment |
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"sspace 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:1get the first character: "u"
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
add a comment |
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"sspace 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:1get the first character: "u"
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
add a comment |
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"sspace 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:1get the first character: "u"
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"sspace 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:1get the first character: "u"
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
add a comment |
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
add a comment |
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
just curious to understand to know, why isdeclare -pneeded 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
add a comment |
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
just curious to understand to know, why isdeclare -pneeded 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
add a comment |
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
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
answered Apr 14 at 12:05
glenn jackmanglenn jackman
53.9k674115
53.9k674115
just curious to understand to know, why isdeclare -pneeded 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
add a comment |
just curious to understand to know, why isdeclare -pneeded 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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