以下sed表达式出了什么问题?我正在尝试获取topic =
和遇到的first semi-colon
之间的所有内容,然后使用逗号作为分隔符拆分为结果。
sed "s/oc.diffusion.topic = ""(.*)"""";/\1/g"| sed "s/$/,/"
这是测试字符串:
xchangeCommission = true;oc.diffusion.topic = "Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/topic,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*/repub_OC//,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/repub_OC/all,Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188//,Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188/all";var SKY_TRA
答案 0 :(得分:1)
使用sed
sed 's/.*oc.diffusion.topic = "\([^"]*\)".*/\1/' file
Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/topic,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*/repub_OC//,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/repub_OC/all,Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188//,Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188/all
[^"]*
会很容易地命令所有字符都没有双引号。 或使用grep
grep -Po "(?<=topic = \")[^\"]*" file
sed 's/,/\n/g;s/.*\"\(.*\)\".*/\1/' file
Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*
Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/topic
Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*/repub_OC//
Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/repub_OC/all
Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188//
Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188/all
答案 1 :(得分:1)
由于您拥有的文字是引号之间的唯一文字,因此您可以使用此简单awk
awk -F\" '{print $2}' file
Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/topic,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*/repub_OC//,Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/repub_OC/all,Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188//,Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188/all
答案 2 :(得分:1)
喜欢这个吗?
sed 's/[^"]*"//;s/".*//;y/,/\n/'
或
awk '!/;/' RS='[",]'
结果
Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.* Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/topic Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_.*/repub_OC// Oddschecker/category_27/event_9945/subevent_63289835/market_1758201893/bet_all/repub_OC/all Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188// Oddschecker/category_27/event_9945/subevent_63289835/fixture_401188/all