开发者

parsing multiple values from a file

I have a file that is just one line (one HUGE line) to parse. I want to parse out the value that appears between "Undefined error code" and " id" on this line. The thing is this appears multiple times on the same line with different values everywhere. The following code only gives me the last instance.

cat bad_events_P2J3.xml | sed -n 's/.*Undefined error code (\(.*\))\" id.*/\1\n/p'

How can I get all instances of 开发者_Go百科this?


You were on the right track:

sed -n 's/.*Undefined error code\(.*\)id.*/\1/p' bad_events_P2J3.xml

Note that cat is unnecessary and, unless you need an extra newline, sed will provide one for you.

I missed the fact that this appears multiple times in your file. This should work in that case:

grep -Po 'Undefined error code.*?id' bad_events_P2J3.xml | sed 's/^Undefined error code//;s/id$//'


$ cat file
text1 text2 Undefined error code text3 text4 id text5 text6 Undefined error code txt7 txt8 id
$ awk -vRS="id" '{gsub(/.*Undefined error code/,"")}1' file
 text3 text4
 txt7 txt8
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜