[英]Linux Shell Script: extract values from string in specified manner
I have one variable contain values like: 我有一个变量包含类似的值:
USER1:USER2,USER3:USER4,USER5:USER6
I want to extract values like USER1,USER3,USER5
我想提取
USER1,USER3,USER5
For Example: 例如:
VALUE = USER1:USER2,USER3:USER4,USER5:USER6
how I echo
to extract like this 我如何
echo
以这样提取
USER1,USER3,USER5
#!/bin/bash
string=USER1:USER2,USER3:USER4,USER5:USER6
IFS=', ' read -r -a array <<< "$string"
output=
for kv in ${array[@]}; do
key=$(echo $kv | cut -d':' -f1)
output="$output,$key"
done
output=$(echo $output | cut -c 2-)
echo $output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.