正则表达式替换空格,字母和数字?

时间:2018-01-12 19:02:06

标签: shell vi

我想知道是否有任何简单的vi命令将以下内容作为输入并获得下面提到的输出?我试图找到一种更简单的方法来获取给定的输入并获得预期的输出? INPUT1:

df.pivot(*df.columns)
Out[648]: 
song_id  SOBYHAJ12A6701BF1D  SODACBL12A8C13C273  SODXRTY12AB0180F3B  \
user_id                                                               
frank                  23.0                 NaN                 NaN   
john                    NaN                 1.0                 3.0   
mary                    NaN                 NaN                 NaN   
song_id  SOFRQTD12A81C233C0  
user_id                      
frank                   NaN  
john                    NaN  
mary                    1.0  

INPUT2:

 CoreCapture    6fabc0f7591ae8d4b73aab1e6875026240d02922 (6fabc0f7)
 IOdmgLoader    ddc205a776a0ff3abb3ccd19b9100c8c4ecc6527 (ddc205a7)
 TestPortFamily 0a6fadcf27a5e424bfdb355428f720c76a0fa0c9 (0a6fadcf)
 TestPortCoreCOM    84521ebf0edcc9f24b004dca7ee59f082912bcc7 (84521ebf)
 TechCloudSyncEngine    3ef603e2c6311ed66619309f305574cfc42d8969 (3ef603e2)
 TestPort   34e12f3229b42b441f4a79297d7257387ec71e47 (34e12f32)
 TechVelocity   e3abcce015a0b656d1895c764572fc5db74a0342 (e3abcce0)
 TechnologyUtilities    01c5333f18ced7e2999f9d81b97e75adf0b52319 (01c5333f)
 SPTestPortReporter 0ded7780e104798e7f6b51fc4acfef1de18cebcb (0ded7780)
 VendorCOM  8b153b85627fd26e09d3c72b08a8a0d057276d02 (8b153b85)
 ComvendorCOMV3 e88c71b7f6eba361961ce90809f81e5c8072b1ed (e88c71b7)
 vendorCOMSoftware_Dingo2_4364_Olympic_Bin  73f8c673914706dd22b9c7e946c350982966732b (73f8c673)
 vendorCOMSoftware_Dingo2_4355_Olympic_Bin  0d3f2b9bf0cde2973f44b64d2da36eda7f042e21 (0d3f2b9b)
 TestPortDrivercommode  8f227d2789c67f82d69f75adb06679c94d56ed33 (8f227d27)
 NetworkUtility 15beb21f8d8aae7281d9dd0460850c01f7a80770 (15beb21f)

输出: -

 CoreCapture    branchname (6fabc0f7)
 IOmazeLoader  branchname (ddc205a7)
 GaaliPortFamily  branchname (0a6fadcf)
 GaaliPortCoreTECH        branchname (84521ebf)
 TechCloudSyncEngine    branchname (3ef603e2)
 GaaliPort        branchname (34e12f32)
 TechVelocity   branchname (e3abcce0)
 ConnUtilities      branchname (01c5333f)
 SPGaaliPortReporter      branchname (0ded7780)
 vendorTECH   branchname (8b153b85)
 ComvendorTECHV3 branchname (0770f3c1)
 vendorTECHFirm_Dingo2_4364_company_Bin        branchname (73f8c673)
 vendorTECHFirm_Dingo2_4355_company_Bin        branchname (0d3f2b9b)
 GaaliPortDriverBrcmNIC   branchname (8f227d27)
 NetworkUtility branchname (15beb21f)
 ConnCoexManager    macOS-Tech-QA-Root (8189ef53)

我尝试在vi编辑器上运行以下命令,但似乎没有用,任何指针都是正确的正则表达式?

CoreCapture:6fabc0f7
IOdmgLoader:ddc205a7
TestPortFamily:0a6fadcf
TestPortCoreCOM:84521ebf
TechCloudSyncEngine:3ef603e2
TestPort:34e12f32
TechVelocity:e3abcce0
TechnologyUtilities:01c5333f
SPTestPortReporter:0ded7780
VendorCOM:8b153b85
ComvendorCOMV3:e88c71b7
vendorCOMSoftware_Dingo2_4364_Olympic_Bin:73f8c673
vendorCOMSoftware_Dingo2_4355_Olympic_Bin:0d3f2b9b
TestPortDrivercommode:8f227d27
NetworkUtility:15beb21f

1 个答案:

答案 0 :(得分:2)

使用vim,您可以使用此替换:

%s/\v^\s*(\S+)\s+\S+\s+\((\S+)\).*/\1:\2/

<强>解释

  • :%s:匹配所有行的替换
  • \v:启用非常神奇的
  • ^:行开始
  • \s*:匹配0个或更多前导空格
  • (\S+):匹配1个以上的非空格字符并将其捕获到组#1
  • \s+:匹配1个以上的空白字符
  • \S+:匹配1个非空格字符
  • \s+:匹配1个以上的空白字符
  • \(:匹配文字(
  • (\S+):匹配1个以上的非空格字符并将其捕获到组#2
  • \):匹配文字)
  • .*:在此之后匹配任何内容

<强>换人:

  • \1:\2:反向引用#1后跟冒号,后跟反引用#2

没有魔法,你可以使用:

%s/^\s*\(\S\+\)\s\+\S\+\s\+(\(\S\+\)).*/\1:\2/