我想解析一个字符串(使用 awk/sed/grep):
"EffTimeStamp=20110203-210000#InputCtxNm=Forigen Exchange Today#RunType=EOD"
结果应该是:
EffTimeStamp="20110203-210000"
InputCtxNm="Forigen Exchange Today"
RunType="EOD"
请指教。
它需要几个不同的替换,但sed
可以做到这一点。
sed 's/^"//; s/=/="/g; s/#/"\n/g'
例如
% echo '"EffTimeStamp=20110203-210000#InputCtxNm=Forigen Exchange Today#RunType=EOD"' |
sed 's/^"//;s/=/="/g;s/#/"\n/g'
EffTimeStamp="20110203-210000"
InputCtxNm="Forigen Exchange Today"
RunType="EOD"
What shell are you using? For bash, you can write
str="EffTimeStamp=20110203-210000#InputCtxNm=Forigen Exchange Today#RunType=EOD"
oifs=$IFS
IFS="#"
set -- $str
IFS=$oifs
printf "%s\n" "$@"
outputs
EffTimeStamp=20110203-210000
InputCtxNm=Forigen Exchange Today
RunType=EOD
If you don't want to use the positional parameters, you can store the values as an array:
arr=($str)
printf "%s\n" "${arr[@]}"
or 和awk解决方案
echo "EffTimeStamp=20110203-210000#InputCtxNm=Forigen Exchange Today#RunType=EOD" \
| awk -F# -v OFS="\n" '{for (i=1;i<=NF; i++) printf("%s%s", $i, (i==NF ? "\n" : OFS)) }'
输出
EffTimeStamp=20110203-210000
InputCtxNm=Forigen Exchange Today
RunType=EOD
我希望这有帮助。
perl
修改$/
(输入记录分隔符)和$\
(输出记录分隔符)的一种简单方法:
echo -e "EffTimeStamp=20110203-210000#InputCtxNm=Forigen Exchange Today#RunType=EOD" |
perl -pe 'BEGIN { $/ = q[#]; $\ = qq[\n] }'
输出:
EffTimeStamp=20110203-210000#
InputCtxNm=Forigen Exchange Today#
RunType=EOD