-1

我想解析一个字符串(使用 awk/sed/grep):

"EffTimeStamp=20110203-210000#InputCtxNm=Forigen Exchange Today#RunType=EOD"

结果应该是:

EffTimeStamp="20110203-210000"
InputCtxNm="Forigen Exchange Today"
RunType="EOD"

请指教。

4

4 回答 4

1

它需要几个不同的替换,但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"
于 2012-05-07T15:28:59.830 回答
0

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[@]}"
于 2012-05-07T15:40:37.397 回答
0

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

我希望这有帮助。

于 2012-05-07T15:42:09.443 回答
0

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
于 2012-05-08T13:41:21.083 回答