我有很多字符串,它们类似于
icon=null restored=0 title=Adblock Browser itemType=0 container=-101
我想替换=
之后的每个字符串,这样就有一个|
来分隔两个字段。
在上面的例子中,我希望字符串被替换为-
icon=null |restored=0 |title=Adblock Browser |itemType=0 |container=-101
在VIM中,我尝试了以下search-and-replace
表达-
:%s/=.{-} ?.{-} / |/gc
然而,这里的问题是,它不匹配=Adblock Browser
,它只匹配=Adblock
部分。
关于我使用.{-}
的部分,是因为有时字符串是这样的-
icon=null profileId=0 screen=0 modified=1462258474716 iconPackage=null iconResource=null spanX=1 cellX=2 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.MainActivity;end restored=0 title=Play Games itemType=0 container=8 iconType=null isShortcut=null spanY=1 _id=14 cellY=1 uri=null appWidgetId=-1
icon=null profileId=0 screen=1 modified=0 iconPackage=null iconResource=null spanX=1 cellX=3 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.android.vending/.AssetBrowserActivity;end restored=0 title=Play Store itemType=0 container=-100 iconType=null isShortcut=null spanY=1 _id=15 cellY=3 uri=null appWidgetId=-1
icon=null profileId=0 screen=4 modified=0 iconPackage=null iconResource=null spanX=1 cellX=4 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=org.adblockplus.browser;component=org.adblockplus.browser/.App;end restored=0 title=Adblock Browser itemType=0 container=-101 iconType=null isShortcut=null spanY=1 _id=19 cellY=0 uri=null appWidgetId=-1
输出应该是这样的-
icon=null |profileId=0 |screen=0 |modified=1462258474716 |iconPackage=null |iconResource=null |spanX=1 |cellX=2 |displayMode=null |appWidgetProvider=null |intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.google.android.play.games/com.google.android.gms.games.ui.destination.main.MainActivity;end |restored=0 |title=Play Games |itemType=0 container=8 |iconType=null |isShortcut=null |spanY=1 |_id=14 |cellY=1 |uri=null |appWidgetId=-1
并且对于其他字符串也是类似的。
最小、可验证的示例
这是输入文件的内容-
icon=null profileId=0 screen=4 modified=0 iconPackage=null iconResource=null spanX=1 cellX=4 displayMode=null appWidgetProvider=null intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=org.adblockplus.browser;component=org.adblockplus.browser/.App;end restored=0 title=Adblock Browser itemType=0 container=-101 iconType=null isShortcut=null spanY=1 _id=19 cellY=0 uri=null appWidgetId=-1
VIM-中的搜索和替换表达式
:%s/=.{-} ?.{-} / |/gc
输出:
icon=null |profileId=0 |screen=4 |modified=0 |iconPackage=null |iconResource=null |spanX=1 |cellX=4 |displayMode=null |appWidgetProvider=null |intent=#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=org.adblockplus.browser;component=org.adblockplus.browser/.App;end |restored=0 |title=Adblock |Browser itemType=0 |container=-101 |iconType=null |isShortcut=null |spanY=1 |_id=19 |cellY=0 |uri=null |appWidgetId=-1
错误部件-
restored=0 |title=Adblock |Browser itemType=0 |container=-101 |
应该是-
restored=0 |title=Adblock Browser |itemType=0 |container=-101 |
sed -r 's/ ([^= ]*=)/ |1/g' file
这将匹配后面跟着0个或多个字符的空白,但空白或后面跟着=
的=
除外。()括号内的字符串捕获将在1
中,即第一个捕获组中。