本文介绍了使用sed获取单词的第一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个bash脚本来打印名称的第一个字母.例如:Ruben Van Den Bosshe成为RVDB或Ken Van de Wilde成为KVdW
I need a bash script that prints the first letter of a name. Example: Ruben Van Den Bosshe becomes RVDB or Ken Van de Wilde becomes KVdW
我要使用sed命令.
推荐答案
可能有更简洁的方法,但是以下方法似乎可行:
There's probably a neater way of doing this, but the following seems to work:
$ echo 'Ken Van de Wilde' | sed 's/\(\w\)\w*\( \|$\)/\1/g'
KVdW
$ echo 'Ruben Van Den Bosshe' | sed 's/\(\w\)\w*\( \|$\)/\1/g'
RVDB
要稍微分解一下该正则表达式,它依次匹配以下内容:
To break down that regular expression a bit, it matches the following in turn:
- 在第一组中捕获的单词字母:
\(\ w \)
- 零个或多个单词字母:
\ w *
- 最后,是空格或行尾:
\(\ | $ \)
该序列将替换为第一组中捕获的所有序列: \ 1
That sequence is replaced with whatever was captured in the first group: \1
这篇关于使用sed获取单词的第一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!