样本文件
ABCD|XYZ_123
CDEF|XYZ_456
GHIJ|ZEE_569
输出:
XYZ_123
XYZ_456
答案1
几个选项:
- Grep 和剪切:
grep XYZ file | cut -d'|' -f2
- (GNU) 仅 Grep:
grep -o 'XYZ.*$' file
- awk:
awk -F'|' '/XYZ/{print $2}' file
- Bash 数组和循环(只是因为我可以):
declare -A array
eval array=( $(printf '[%s]=%s ' $(tr '|' ' ' < file)) )
for v in ${array[@]}
do
[[ $v =~ XYZ ]] && echo $v
done