开发者

How can I extract the part of a line included between ":"?

I am having trouble parsing this particular line using sed:

/media/file/1.bmp app:Stuff I want:

Basically I want to get the stuff in between the two colons (::), i.e. Stuff I 开发者_运维百科want in this case.

I tried

sed -r 's/.*app:([\s\w\d]*):.*/\1/'

This didnt work.


Try using the following (update: appears \: isn't necessary, : is fine)

sed -r 's/.*\:([^\:]*)\:.*/\1/'

or per @brandizzi and @joemooney's answer:

sed -r 's/.*:([^:]*):.*/\1'

or with cut

cut -f 2 -d":"


You don't need sed for that, awk looks nicer:

awk -F : '{print $2}'


$ echo "/media/file/1.bmp app:Stuff I want:" | sed -r 's/.*app:([^:]*):.*/\1/'
Stuff I want


echo '/media/file/1.bmp app:Stuff I want:' | cut -d ':' -f 2

Simple and elegant. Cut is the tool I use for deliminating fields. -d notes the deliminating character, -f 2 tells you to get field 2.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜