如何使用bash查找字符串中包含的單詞的第一個字母。查找字符串中每個單詞的第一個字母
例如
代碼:
str="my-custom-string'
我希望找到M,C,S。我知道如何找到第一個字母,但這稍微複雜一些。 非常感謝,
如何使用bash查找字符串中包含的單詞的第一個字母。查找字符串中每個單詞的第一個字母
例如
代碼:
str="my-custom-string'
我希望找到M,C,S。我知道如何找到第一個字母,但這稍微複雜一些。 非常感謝,
$ echo 'my-custom-string' | egrep -o '\b\w'
m
c
s
使用參數替換的純Bash。刪除減,選擇每個單詞的第一個字符:
str="my-custom-string"
for word in ${str//-/ }; do
echo "${word:0:1}"
done
結果
m
c
s
這裏有一個sed
版本:
echo 'my-custom-string' | sed 's/\(^\|-\)\(.\)[^-]*/\2\n/g'
這可能會爲你工作(GNU SED)
echo 'my-custom-string' | sed 's/\B.//g;y/-/,/'
m,c,s
或:
echo 'my-custom-string' | sed 's/\B.//g;y/-/\n/'
m
c
s
「我知道如何尋找的第一個字母」,並沒有告訴我們,向我們展示[你已經試過(http://whathaveyoutried.com/ )和你卡住的地方。 – Johnsyweb 2012-07-08 10:35:36
str ='my-custom-string'; echo $ {str:0:1} – 2012-07-08 11:13:10