Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  147] [ 0]  / answers: 1 / hits: 380940  / 2 Years ago, sat, august 6, 2022, 7:56:11

I have a string like that:



|abcdefg|


And I want to get a new string called in someway (like string2) with the original string without the two | characters at the start and at the end of it so that I will have this:



abcdefg


Is that possible in bash?


More From » bash

 Answers
3

You can do



string="|abcdefg|"
string2=${string#"|"}
string2=${string2%"|"}
echo $string2


Or if your string length is constant, you can do



string="|abcdefg|"
string2=${string:1:7}
echo $string2


Also, this should work



echo "|abcdefg|" | cut -d "|" -f 2


Also this



echo "|abcdefg|" | sed 's/^|(.*)|$/1/'

[#41321] Sunday, August 7, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
erranbe

Total Points: 118
Total Questions: 95
Total Answers: 117

Location: Virgin Islands (U.S.)
Member since Tue, Jul 7, 2020
4 Years ago
;