开发者

Moving text using sed

I have a xml file with following contents:

<query_1>false</quer开发者_Python百科y_1>
<query_2>true</query_2>
<query_3>true</query_3>
<query_4>false</query_4>
<query_5>false</query_5>

Is it possible to reorganize the file using sed, so it would pick up "true" and outputs them into one line like this:

<query>2,3</query>

Regards, Irek


I guess it's not the simplest solution, but you could try this:

sed -n '/<query_[0-9]*>true<\/query/{:a;s/^<query_\([0-9]*\)>.*/\1/;tb;ba;:b;H};${g;s/^\n//;s/\n/,/g};$s/.*/<query>&<\/query>/p' file

The output using your example file is exactly what you want:

<query>2,3</query>


The following sequences of commands returns the string you request

$ grep true input | sed 's/>//g' | cut -d _ -f 3 | printf "<query>%s</query>" `paste -d, - -`
<query>2,3</query>

Gives:

<query>2,3</query>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜