How to get single character after space?
.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 yesterday
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нιη
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
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"
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"
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
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 -p
needed here? The array is already populated right?
– Inian
10 mins ago
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нιη
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
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нιη
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
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 22 hours ago
answered 22 hours ago
Kusalananda♦Kusalananda
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
add a comment |
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
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"
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"
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
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"
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"
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
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"
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"
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"
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
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
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
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 -p
needed here? The array is already populated right?
– Inian
10 mins ago
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 -p
needed here? The array is already populated right?
– Inian
10 mins ago
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 17 hours ago
glenn jackmanglenn jackman
53.1k573114
53.1k573114
just curious to understand to know, why isdeclare -p
needed here? The array is already populated right?
– Inian
10 mins ago
add a comment |
just curious to understand to know, why isdeclare -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
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