Convert only certain words to lowercaseWhy is printf better than echo?Why does my shell script choke on whitespace or other special characters?Find a string in one section of a file with mutiple sectionsHow to replace the content of specific column with awk? Tab Delimited FileIssues with storing an echo of a date conversion into a string variable inunixprint only lines where the first column is uniqueUse sed to retrieve variable from 1 file and place it in anotherHow to find/grep what is between string1 and string2?sed use from android shellusing awk with variablesHow to use sed to replace a linux path in a specific text fileHow to convert multiline to singleline but preserve paragraphs
If the mass of the Earth is decreasing by sending debris in space, does its angular momentum also decrease?
How did the European Union reach the figure of 3% as a maximum allowed deficit?
I'm yearning in grey
Build a scale without computer
What is this airplane that sits in front of Barringer High School in Newark, NJ?
Why swap space doesn't get filesystem check at boot time?
Expand command in an argument before the main command
How do I become a better writer when I hate reading?
Why we can't jump without bending our knees?
Justifying Affordable Bespoke Spaceships
Harmonic Series Phase Difference?
Is a sequel allowed to start before the end of the first book?
What is "dot" sign in •NO?
How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?
Probability Dilemma
What are the mechanical differences between Adapt and Monstrosity?
Do battery electrons only move if there is a positive terminal at the end of the wire?
What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?
Leaving job close to major deadlines
Checking if argument is a floating point without breaking on control sequences in argument
Is swap gate equivalent to just exchanging the wire of the two qubits?
Does cooling a potato change the nature of its carbohydrates?
How do I correctly reduce geometry on part of a mesh?
Scaling an object to change its key
Convert only certain words to lowercase
Why is printf better than echo?Why does my shell script choke on whitespace or other special characters?Find a string in one section of a file with mutiple sectionsHow to replace the content of specific column with awk? Tab Delimited FileIssues with storing an echo of a date conversion into a string variable inunixprint only lines where the first column is uniqueUse sed to retrieve variable from 1 file and place it in anotherHow to find/grep what is between string1 and string2?sed use from android shellusing awk with variablesHow to use sed to replace a linux path in a specific text fileHow to convert multiline to singleline but preserve paragraphs
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Say I have a variable var
whose value is fOo bar1 baR2 bArab
.
How do I go about saving into another variable, say, lc_var
, a version of var
where all but the first word are converted to lowercase?
I know it can look something like lc_var=$(echo $var | ...)
, where ...
will replaced by an appropriate AWK or sed command.
text-processing variable
New contributor
add a comment |
Say I have a variable var
whose value is fOo bar1 baR2 bArab
.
How do I go about saving into another variable, say, lc_var
, a version of var
where all but the first word are converted to lowercase?
I know it can look something like lc_var=$(echo $var | ...)
, where ...
will replaced by an appropriate AWK or sed command.
text-processing variable
New contributor
add a comment |
Say I have a variable var
whose value is fOo bar1 baR2 bArab
.
How do I go about saving into another variable, say, lc_var
, a version of var
where all but the first word are converted to lowercase?
I know it can look something like lc_var=$(echo $var | ...)
, where ...
will replaced by an appropriate AWK or sed command.
text-processing variable
New contributor
Say I have a variable var
whose value is fOo bar1 baR2 bArab
.
How do I go about saving into another variable, say, lc_var
, a version of var
where all but the first word are converted to lowercase?
I know it can look something like lc_var=$(echo $var | ...)
, where ...
will replaced by an appropriate AWK or sed command.
text-processing variable
text-processing variable
New contributor
New contributor
New contributor
asked Jun 9 at 19:49
lowercaselowercase
111
111
New contributor
New contributor
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
With the zsh
shell:
set -o extendedglob
lc_var=$var/%(#m) */$(L)MATCH
Where $var/%pattern/replacement
like in ksh replaces the end of the var that matches the pattern
with the replacement
, (#m)
causes the matched portion to be stored in $MATCH
(that's the part that needs extendedglob
) and $(L)MATCH
converts $MATCH
to lowercase.
With the bash
shell:
tmp1=$var%% *
tmp2=$var#"$tmp1"
lc_var=$tmp1$tmp2,,
POSIXly, you'd do:
lc_var=$(
awk '
BEGIN
var = ARGV[1]
if (i = index(var, " "))
var = substr(var, 1, i) tolower(substr(var, i + 1))
print var "."
' "$var"
)
lc_var=$lc_var%.
In any case, that can't look like lc_var=$(echo $var ...)
because $(...)
is POSIX/Korn/bash/zsh shell syntax, and in those shells, echo
can't output arbitrary data and variable expansions should be quoted (except maybe in zsh
).
All those variants turn to lower case all the characters past the first space character in $var
(making no assumption on what characters they may be). You'd need to adapt it if words are possibly delimited by characters other than space, or if there may be word delimiter characters before the first word.
Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.
– seshoumara
Jun 10 at 7:10
1
@seshoumara,awk
doesn't process any input when there are onlyBEGIN
statements (and optional function definitions).
– Stéphane Chazelas
Jun 10 at 7:11
Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.
– seshoumara
Jun 10 at 7:28
@seshoumara, GNUsed
can do it withL
as you've shown (that syntax coming fromvi
/ex
initially IIRC), POSIXsed
can do it withy
but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)
– Stéphane Chazelas
Jun 10 at 7:31
I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.
– seshoumara
Jun 10 at 7:37
|
show 2 more comments
With bash
:
var='fOo bar1 baR2 bArab'
tail=$var#* ; lc_var="$var%% * $tail,,"
echo "$lc_var"
fOo bar1 bar2 barab
as a function:
set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,";
or:
set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t;
(this 2nd version should also work in ksh
, and will handle an argument made up of a single word)
set_lc var 'FOO BAR BAZ'
echo "$var"
FOO bar baz
More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.
That doesn't work properly if the variable contains only one word (doesn't contain any space character)
– Stéphane Chazelas
Jun 10 at 5:03
The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotlessI
in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use oftypeset -l
and$var,,
.
– pizdelect
Jun 10 at 5:55
add a comment |
The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.
lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
lc_var="$lc_var%."
Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.
@StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.
– seshoumara
Jun 10 at 11:05
add a comment |
lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')
(with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)
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
);
);
lowercase is a new contributor. Be nice, and check out our Code of Conduct.
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%2f523880%2fconvert-only-certain-words-to-lowercase%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
With the zsh
shell:
set -o extendedglob
lc_var=$var/%(#m) */$(L)MATCH
Where $var/%pattern/replacement
like in ksh replaces the end of the var that matches the pattern
with the replacement
, (#m)
causes the matched portion to be stored in $MATCH
(that's the part that needs extendedglob
) and $(L)MATCH
converts $MATCH
to lowercase.
With the bash
shell:
tmp1=$var%% *
tmp2=$var#"$tmp1"
lc_var=$tmp1$tmp2,,
POSIXly, you'd do:
lc_var=$(
awk '
BEGIN
var = ARGV[1]
if (i = index(var, " "))
var = substr(var, 1, i) tolower(substr(var, i + 1))
print var "."
' "$var"
)
lc_var=$lc_var%.
In any case, that can't look like lc_var=$(echo $var ...)
because $(...)
is POSIX/Korn/bash/zsh shell syntax, and in those shells, echo
can't output arbitrary data and variable expansions should be quoted (except maybe in zsh
).
All those variants turn to lower case all the characters past the first space character in $var
(making no assumption on what characters they may be). You'd need to adapt it if words are possibly delimited by characters other than space, or if there may be word delimiter characters before the first word.
Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.
– seshoumara
Jun 10 at 7:10
1
@seshoumara,awk
doesn't process any input when there are onlyBEGIN
statements (and optional function definitions).
– Stéphane Chazelas
Jun 10 at 7:11
Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.
– seshoumara
Jun 10 at 7:28
@seshoumara, GNUsed
can do it withL
as you've shown (that syntax coming fromvi
/ex
initially IIRC), POSIXsed
can do it withy
but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)
– Stéphane Chazelas
Jun 10 at 7:31
I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.
– seshoumara
Jun 10 at 7:37
|
show 2 more comments
With the zsh
shell:
set -o extendedglob
lc_var=$var/%(#m) */$(L)MATCH
Where $var/%pattern/replacement
like in ksh replaces the end of the var that matches the pattern
with the replacement
, (#m)
causes the matched portion to be stored in $MATCH
(that's the part that needs extendedglob
) and $(L)MATCH
converts $MATCH
to lowercase.
With the bash
shell:
tmp1=$var%% *
tmp2=$var#"$tmp1"
lc_var=$tmp1$tmp2,,
POSIXly, you'd do:
lc_var=$(
awk '
BEGIN
var = ARGV[1]
if (i = index(var, " "))
var = substr(var, 1, i) tolower(substr(var, i + 1))
print var "."
' "$var"
)
lc_var=$lc_var%.
In any case, that can't look like lc_var=$(echo $var ...)
because $(...)
is POSIX/Korn/bash/zsh shell syntax, and in those shells, echo
can't output arbitrary data and variable expansions should be quoted (except maybe in zsh
).
All those variants turn to lower case all the characters past the first space character in $var
(making no assumption on what characters they may be). You'd need to adapt it if words are possibly delimited by characters other than space, or if there may be word delimiter characters before the first word.
Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.
– seshoumara
Jun 10 at 7:10
1
@seshoumara,awk
doesn't process any input when there are onlyBEGIN
statements (and optional function definitions).
– Stéphane Chazelas
Jun 10 at 7:11
Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.
– seshoumara
Jun 10 at 7:28
@seshoumara, GNUsed
can do it withL
as you've shown (that syntax coming fromvi
/ex
initially IIRC), POSIXsed
can do it withy
but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)
– Stéphane Chazelas
Jun 10 at 7:31
I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.
– seshoumara
Jun 10 at 7:37
|
show 2 more comments
With the zsh
shell:
set -o extendedglob
lc_var=$var/%(#m) */$(L)MATCH
Where $var/%pattern/replacement
like in ksh replaces the end of the var that matches the pattern
with the replacement
, (#m)
causes the matched portion to be stored in $MATCH
(that's the part that needs extendedglob
) and $(L)MATCH
converts $MATCH
to lowercase.
With the bash
shell:
tmp1=$var%% *
tmp2=$var#"$tmp1"
lc_var=$tmp1$tmp2,,
POSIXly, you'd do:
lc_var=$(
awk '
BEGIN
var = ARGV[1]
if (i = index(var, " "))
var = substr(var, 1, i) tolower(substr(var, i + 1))
print var "."
' "$var"
)
lc_var=$lc_var%.
In any case, that can't look like lc_var=$(echo $var ...)
because $(...)
is POSIX/Korn/bash/zsh shell syntax, and in those shells, echo
can't output arbitrary data and variable expansions should be quoted (except maybe in zsh
).
All those variants turn to lower case all the characters past the first space character in $var
(making no assumption on what characters they may be). You'd need to adapt it if words are possibly delimited by characters other than space, or if there may be word delimiter characters before the first word.
With the zsh
shell:
set -o extendedglob
lc_var=$var/%(#m) */$(L)MATCH
Where $var/%pattern/replacement
like in ksh replaces the end of the var that matches the pattern
with the replacement
, (#m)
causes the matched portion to be stored in $MATCH
(that's the part that needs extendedglob
) and $(L)MATCH
converts $MATCH
to lowercase.
With the bash
shell:
tmp1=$var%% *
tmp2=$var#"$tmp1"
lc_var=$tmp1$tmp2,,
POSIXly, you'd do:
lc_var=$(
awk '
BEGIN
var = ARGV[1]
if (i = index(var, " "))
var = substr(var, 1, i) tolower(substr(var, i + 1))
print var "."
' "$var"
)
lc_var=$lc_var%.
In any case, that can't look like lc_var=$(echo $var ...)
because $(...)
is POSIX/Korn/bash/zsh shell syntax, and in those shells, echo
can't output arbitrary data and variable expansions should be quoted (except maybe in zsh
).
All those variants turn to lower case all the characters past the first space character in $var
(making no assumption on what characters they may be). You'd need to adapt it if words are possibly delimited by characters other than space, or if there may be word delimiter characters before the first word.
edited Jun 10 at 7:16
answered Jun 9 at 20:23
Stéphane ChazelasStéphane Chazelas
322k57614985
322k57614985
Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.
– seshoumara
Jun 10 at 7:10
1
@seshoumara,awk
doesn't process any input when there are onlyBEGIN
statements (and optional function definitions).
– Stéphane Chazelas
Jun 10 at 7:11
Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.
– seshoumara
Jun 10 at 7:28
@seshoumara, GNUsed
can do it withL
as you've shown (that syntax coming fromvi
/ex
initially IIRC), POSIXsed
can do it withy
but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)
– Stéphane Chazelas
Jun 10 at 7:31
I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.
– seshoumara
Jun 10 at 7:37
|
show 2 more comments
Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.
– seshoumara
Jun 10 at 7:10
1
@seshoumara,awk
doesn't process any input when there are onlyBEGIN
statements (and optional function definitions).
– Stéphane Chazelas
Jun 10 at 7:11
Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.
– seshoumara
Jun 10 at 7:28
@seshoumara, GNUsed
can do it withL
as you've shown (that syntax coming fromvi
/ex
initially IIRC), POSIXsed
can do it withy
but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)
– Stéphane Chazelas
Jun 10 at 7:31
I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.
– seshoumara
Jun 10 at 7:37
Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.
– seshoumara
Jun 10 at 7:10
Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.
– seshoumara
Jun 10 at 7:10
1
1
@seshoumara,
awk
doesn't process any input when there are only BEGIN
statements (and optional function definitions).– Stéphane Chazelas
Jun 10 at 7:11
@seshoumara,
awk
doesn't process any input when there are only BEGIN
statements (and optional function definitions).– Stéphane Chazelas
Jun 10 at 7:11
Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.
– seshoumara
Jun 10 at 7:28
Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.
– seshoumara
Jun 10 at 7:28
@seshoumara, GNU
sed
can do it with L
as you've shown (that syntax coming from vi
/ex
initially IIRC), POSIX sed
can do it with y
but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)– Stéphane Chazelas
Jun 10 at 7:31
@seshoumara, GNU
sed
can do it with L
as you've shown (that syntax coming from vi
/ex
initially IIRC), POSIX sed
can do it with y
but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)– Stéphane Chazelas
Jun 10 at 7:31
I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.
– seshoumara
Jun 10 at 7:37
I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.
– seshoumara
Jun 10 at 7:37
|
show 2 more comments
With bash
:
var='fOo bar1 baR2 bArab'
tail=$var#* ; lc_var="$var%% * $tail,,"
echo "$lc_var"
fOo bar1 bar2 barab
as a function:
set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,";
or:
set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t;
(this 2nd version should also work in ksh
, and will handle an argument made up of a single word)
set_lc var 'FOO BAR BAZ'
echo "$var"
FOO bar baz
More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.
That doesn't work properly if the variable contains only one word (doesn't contain any space character)
– Stéphane Chazelas
Jun 10 at 5:03
The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotlessI
in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use oftypeset -l
and$var,,
.
– pizdelect
Jun 10 at 5:55
add a comment |
With bash
:
var='fOo bar1 baR2 bArab'
tail=$var#* ; lc_var="$var%% * $tail,,"
echo "$lc_var"
fOo bar1 bar2 barab
as a function:
set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,";
or:
set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t;
(this 2nd version should also work in ksh
, and will handle an argument made up of a single word)
set_lc var 'FOO BAR BAZ'
echo "$var"
FOO bar baz
More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.
That doesn't work properly if the variable contains only one word (doesn't contain any space character)
– Stéphane Chazelas
Jun 10 at 5:03
The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotlessI
in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use oftypeset -l
and$var,,
.
– pizdelect
Jun 10 at 5:55
add a comment |
With bash
:
var='fOo bar1 baR2 bArab'
tail=$var#* ; lc_var="$var%% * $tail,,"
echo "$lc_var"
fOo bar1 bar2 barab
as a function:
set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,";
or:
set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t;
(this 2nd version should also work in ksh
, and will handle an argument made up of a single word)
set_lc var 'FOO BAR BAZ'
echo "$var"
FOO bar baz
More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.
With bash
:
var='fOo bar1 baR2 bArab'
tail=$var#* ; lc_var="$var%% * $tail,,"
echo "$lc_var"
fOo bar1 bar2 barab
as a function:
set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,";
or:
set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t;
(this 2nd version should also work in ksh
, and will handle an argument made up of a single word)
set_lc var 'FOO BAR BAZ'
echo "$var"
FOO bar baz
More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.
edited Jun 9 at 21:31
answered Jun 9 at 20:36
pizdelectpizdelect
1,316311
1,316311
That doesn't work properly if the variable contains only one word (doesn't contain any space character)
– Stéphane Chazelas
Jun 10 at 5:03
The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotlessI
in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use oftypeset -l
and$var,,
.
– pizdelect
Jun 10 at 5:55
add a comment |
That doesn't work properly if the variable contains only one word (doesn't contain any space character)
– Stéphane Chazelas
Jun 10 at 5:03
The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotlessI
in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use oftypeset -l
and$var,,
.
– pizdelect
Jun 10 at 5:55
That doesn't work properly if the variable contains only one word (doesn't contain any space character)
– Stéphane Chazelas
Jun 10 at 5:03
That doesn't work properly if the variable contains only one word (doesn't contain any space character)
– Stéphane Chazelas
Jun 10 at 5:03
The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless
I
in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l
and $var,,
.– pizdelect
Jun 10 at 5:55
The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless
I
in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l
and $var,,
.– pizdelect
Jun 10 at 5:55
add a comment |
The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.
lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
lc_var="$lc_var%."
Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.
@StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.
– seshoumara
Jun 10 at 11:05
add a comment |
The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.
lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
lc_var="$lc_var%."
Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.
@StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.
– seshoumara
Jun 10 at 11:05
add a comment |
The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.
lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
lc_var="$lc_var%."
Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.
The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.
lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
lc_var="$lc_var%."
Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.
edited Jun 10 at 11:01
answered Jun 9 at 19:59
seshoumaraseshoumara
45637
45637
@StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.
– seshoumara
Jun 10 at 11:05
add a comment |
@StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.
– seshoumara
Jun 10 at 11:05
@StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.
– seshoumara
Jun 10 at 11:05
@StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.
– seshoumara
Jun 10 at 11:05
add a comment |
lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')
(with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)
add a comment |
lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')
(with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)
add a comment |
lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')
(with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)
lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')
(with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)
edited Jun 10 at 14:47
Jeff Schaller♦
47k1167152
47k1167152
answered Jun 10 at 12:24
David YockeyDavid Yockey
1287
1287
add a comment |
add a comment |
lowercase is a new contributor. Be nice, and check out our Code of Conduct.
lowercase is a new contributor. Be nice, and check out our Code of Conduct.
lowercase is a new contributor. Be nice, and check out our Code of Conduct.
lowercase is a new contributor. Be nice, and check out our Code of Conduct.
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%2f523880%2fconvert-only-certain-words-to-lowercase%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