bash 中的子字符串
Substring string in bash
我得到这个字符串:
xavier.blodot
wisoyo.hadi
我想要这个输出:名字+姓氏
xblodot
whadi
问候!!
你应该使用这种方法:
sed -r 's:(.).*\.(.+):\.:g' {YOUR_FILE.TXT}
再小一点sed
:
sed -E 's/^(.)[^.]+\.//' file
xblodot
whadi
或使用awk
:
awk -F. '{print substr(,1,1) }' file
xblodot
whadi
标题说在bash
,所以我假设它必须在bash
仅 (不使用 sed
、awk
或其他外部进程):
while read ns; do
echo "${ns::1}${ns#*.}"
done < the_input_file.txt
如果需要,让它更有弹性,取决于您。这取决于您(不)信任输入的程度。例如,这可能包括 IFS= read -r ns
、[[ "$ns" == +([a-z]).+([a-z]) ]]
的检查以及任意其他一致性检查。
name=xavier.blodot
shortened_name="${name:0:1}${name##*.}"
当名称不包含句点时,您必须注意大小写。
我得到这个字符串:
xavier.blodot
wisoyo.hadi
我想要这个输出:名字+姓氏
xblodot
whadi
问候!!
你应该使用这种方法:
sed -r 's:(.).*\.(.+):\.:g' {YOUR_FILE.TXT}
再小一点sed
:
sed -E 's/^(.)[^.]+\.//' file
xblodot
whadi
或使用awk
:
awk -F. '{print substr(,1,1) }' file
xblodot
whadi
标题说在bash
,所以我假设它必须在bash
仅 (不使用 sed
、awk
或其他外部进程):
while read ns; do
echo "${ns::1}${ns#*.}"
done < the_input_file.txt
如果需要,让它更有弹性,取决于您。这取决于您(不)信任输入的程度。例如,这可能包括 IFS= read -r ns
、[[ "$ns" == +([a-z]).+([a-z]) ]]
的检查以及任意其他一致性检查。
name=xavier.blodot
shortened_name="${name:0:1}${name##*.}"
当名称不包含句点时,您必须注意大小写。