How to trim the spaces from first and last of the variable string in Linux? [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












I am trying to remove white spaces from each variable string. I tried with below command but no use.



a="HARISH , SAM"
echo $a|sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'









share|improve this question















closed as unclear what you're asking by Kusalananda, Jeff Schaller, G-Man, RalfFriedl, JigglyNaga Dec 6 at 10:08


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    your desired output?
    – msp9011
    Dec 5 at 13:11










  • Are there other spaces, that you want to keep (for example between words)?
    – sudodus
    Dec 5 at 13:22










  • What if the person's name is HARISH , SAM MIDDLENAME?
    – Jeff Schaller
    Dec 5 at 13:39






  • 2




    Your string does not have any whitespace at the start or end to trim. This question is unclear.
    – Kusalananda
    Dec 5 at 15:50














up vote
-1
down vote

favorite












I am trying to remove white spaces from each variable string. I tried with below command but no use.



a="HARISH , SAM"
echo $a|sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'









share|improve this question















closed as unclear what you're asking by Kusalananda, Jeff Schaller, G-Man, RalfFriedl, JigglyNaga Dec 6 at 10:08


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    your desired output?
    – msp9011
    Dec 5 at 13:11










  • Are there other spaces, that you want to keep (for example between words)?
    – sudodus
    Dec 5 at 13:22










  • What if the person's name is HARISH , SAM MIDDLENAME?
    – Jeff Schaller
    Dec 5 at 13:39






  • 2




    Your string does not have any whitespace at the start or end to trim. This question is unclear.
    – Kusalananda
    Dec 5 at 15:50












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am trying to remove white spaces from each variable string. I tried with below command but no use.



a="HARISH , SAM"
echo $a|sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'









share|improve this question















I am trying to remove white spaces from each variable string. I tried with below command but no use.



a="HARISH , SAM"
echo $a|sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'






linux sed variable whitespace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 at 14:30









Rui F Ribeiro

38.6k1479128




38.6k1479128










asked Dec 5 at 13:08









Harish a

233




233




closed as unclear what you're asking by Kusalananda, Jeff Schaller, G-Man, RalfFriedl, JigglyNaga Dec 6 at 10:08


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Kusalananda, Jeff Schaller, G-Man, RalfFriedl, JigglyNaga Dec 6 at 10:08


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2




    your desired output?
    – msp9011
    Dec 5 at 13:11










  • Are there other spaces, that you want to keep (for example between words)?
    – sudodus
    Dec 5 at 13:22










  • What if the person's name is HARISH , SAM MIDDLENAME?
    – Jeff Schaller
    Dec 5 at 13:39






  • 2




    Your string does not have any whitespace at the start or end to trim. This question is unclear.
    – Kusalananda
    Dec 5 at 15:50












  • 2




    your desired output?
    – msp9011
    Dec 5 at 13:11










  • Are there other spaces, that you want to keep (for example between words)?
    – sudodus
    Dec 5 at 13:22










  • What if the person's name is HARISH , SAM MIDDLENAME?
    – Jeff Schaller
    Dec 5 at 13:39






  • 2




    Your string does not have any whitespace at the start or end to trim. This question is unclear.
    – Kusalananda
    Dec 5 at 15:50







2




2




your desired output?
– msp9011
Dec 5 at 13:11




your desired output?
– msp9011
Dec 5 at 13:11












Are there other spaces, that you want to keep (for example between words)?
– sudodus
Dec 5 at 13:22




Are there other spaces, that you want to keep (for example between words)?
– sudodus
Dec 5 at 13:22












What if the person's name is HARISH , SAM MIDDLENAME?
– Jeff Schaller
Dec 5 at 13:39




What if the person's name is HARISH , SAM MIDDLENAME?
– Jeff Schaller
Dec 5 at 13:39




2




2




Your string does not have any whitespace at the start or end to trim. This question is unclear.
– Kusalananda
Dec 5 at 15:50




Your string does not have any whitespace at the start or end to trim. This question is unclear.
– Kusalananda
Dec 5 at 15:50










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Try this,



 echo $a | sed -e 's/ //g'


you can also try if you have first name and last name



echo $a | sed -e 's/ , /,/g'





share|improve this answer






















  • It is working fine. Thank you.
    – Harish a
    Dec 5 at 13:34










  • Above command supports if we have spaces begining, ending and between the string, but if we don't remove between strings like Frist and Second Name then it won't support.
    – Harish a
    Dec 5 at 13:37






  • 1




    It is working. Thanks again.
    – Harish a
    Dec 5 at 13:44

















up vote
0
down vote













Assuming with "variable string in Linux" you mean a shell variable, why not use shell's "Parameter Expansion", "Pattern Substitution"? Like



$ a="HARISH , SAM"
$ echo $a//
HARISH,SAM





share|improve this answer




















  • this would remove the space between the first name and surname.
    – msp9011
    Dec 5 at 15:09

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Try this,



 echo $a | sed -e 's/ //g'


you can also try if you have first name and last name



echo $a | sed -e 's/ , /,/g'





share|improve this answer






















  • It is working fine. Thank you.
    – Harish a
    Dec 5 at 13:34










  • Above command supports if we have spaces begining, ending and between the string, but if we don't remove between strings like Frist and Second Name then it won't support.
    – Harish a
    Dec 5 at 13:37






  • 1




    It is working. Thanks again.
    – Harish a
    Dec 5 at 13:44














up vote
0
down vote



accepted










Try this,



 echo $a | sed -e 's/ //g'


you can also try if you have first name and last name



echo $a | sed -e 's/ , /,/g'





share|improve this answer






















  • It is working fine. Thank you.
    – Harish a
    Dec 5 at 13:34










  • Above command supports if we have spaces begining, ending and between the string, but if we don't remove between strings like Frist and Second Name then it won't support.
    – Harish a
    Dec 5 at 13:37






  • 1




    It is working. Thanks again.
    – Harish a
    Dec 5 at 13:44












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Try this,



 echo $a | sed -e 's/ //g'


you can also try if you have first name and last name



echo $a | sed -e 's/ , /,/g'





share|improve this answer














Try this,



 echo $a | sed -e 's/ //g'


you can also try if you have first name and last name



echo $a | sed -e 's/ , /,/g'






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 5 at 13:40

























answered Dec 5 at 13:12









msp9011

3,65543863




3,65543863











  • It is working fine. Thank you.
    – Harish a
    Dec 5 at 13:34










  • Above command supports if we have spaces begining, ending and between the string, but if we don't remove between strings like Frist and Second Name then it won't support.
    – Harish a
    Dec 5 at 13:37






  • 1




    It is working. Thanks again.
    – Harish a
    Dec 5 at 13:44
















  • It is working fine. Thank you.
    – Harish a
    Dec 5 at 13:34










  • Above command supports if we have spaces begining, ending and between the string, but if we don't remove between strings like Frist and Second Name then it won't support.
    – Harish a
    Dec 5 at 13:37






  • 1




    It is working. Thanks again.
    – Harish a
    Dec 5 at 13:44















It is working fine. Thank you.
– Harish a
Dec 5 at 13:34




It is working fine. Thank you.
– Harish a
Dec 5 at 13:34












Above command supports if we have spaces begining, ending and between the string, but if we don't remove between strings like Frist and Second Name then it won't support.
– Harish a
Dec 5 at 13:37




Above command supports if we have spaces begining, ending and between the string, but if we don't remove between strings like Frist and Second Name then it won't support.
– Harish a
Dec 5 at 13:37




1




1




It is working. Thanks again.
– Harish a
Dec 5 at 13:44




It is working. Thanks again.
– Harish a
Dec 5 at 13:44












up vote
0
down vote













Assuming with "variable string in Linux" you mean a shell variable, why not use shell's "Parameter Expansion", "Pattern Substitution"? Like



$ a="HARISH , SAM"
$ echo $a//
HARISH,SAM





share|improve this answer




















  • this would remove the space between the first name and surname.
    – msp9011
    Dec 5 at 15:09














up vote
0
down vote













Assuming with "variable string in Linux" you mean a shell variable, why not use shell's "Parameter Expansion", "Pattern Substitution"? Like



$ a="HARISH , SAM"
$ echo $a//
HARISH,SAM





share|improve this answer




















  • this would remove the space between the first name and surname.
    – msp9011
    Dec 5 at 15:09












up vote
0
down vote










up vote
0
down vote









Assuming with "variable string in Linux" you mean a shell variable, why not use shell's "Parameter Expansion", "Pattern Substitution"? Like



$ a="HARISH , SAM"
$ echo $a//
HARISH,SAM





share|improve this answer












Assuming with "variable string in Linux" you mean a shell variable, why not use shell's "Parameter Expansion", "Pattern Substitution"? Like



$ a="HARISH , SAM"
$ echo $a//
HARISH,SAM






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 5 at 15:04









RudiC

3,9091312




3,9091312











  • this would remove the space between the first name and surname.
    – msp9011
    Dec 5 at 15:09
















  • this would remove the space between the first name and surname.
    – msp9011
    Dec 5 at 15:09















this would remove the space between the first name and surname.
– msp9011
Dec 5 at 15:09




this would remove the space between the first name and surname.
– msp9011
Dec 5 at 15:09


Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)