记录一些vi常用的操作,如替换等操作,便于在服务器端更好的处理文本信息。
1 替换操作
1. 命令
(1)格式
:%s/str1/str2/g
(2)解释
参数 | 解析 |
str1 | 表示查找的字符串,可以是正则表达式 |
str2 | 表示的是要替换的字符串 |
g | 在任意位置都做替换 |
2. 举例1
(1)要求
有如下数据
1 |
ENT00050227 {"address":"襄州区二汽基地1号门富康门面房11号","areaNamePath":"中国>华中>湖北>襄阳>襄州区>智慧树","areaPath":"Top.26970.26988.27007.27744.27745.408537","city":"襄阳","cityCode":"xiangzhou_xiang fan","containSku":false,"corePeriod":[],"country":"中国","county":"襄州区","forCommercial":false,"id":408537,"lastMod":1432570828955,"mapId":-1,"name":"智慧树","namePinyin":"zhihuishu","namePy":"zhs","parentSi ghtId":27745,"phoneNumber":"3320857","photoId":-1,"province":"湖北","score":0,"sightCategory":"普通","sightCount":-1,"sightImages":[],"sightTags":[{"code":"s00115","name":"早教俱乐部"}],"sightUtilInfos":[{"inf os":[],"typeId":504,"typeName":"入园参考"},{"infos":[],"typeId":407,"typeName":"交通和到达"},{"infos":[],"typeId":505,"typeName":"温馨提示"}],"status":"offline","ticketThemeExt":"s00115","ticketThemeExtAsList" :["s00115"],"type":"门店","version":3923317,"website":"http://xf.meituan.com/shop/1553138"} |
需要获取到ENT0005027和ticketThemeExt的值:
1 |
ENT00050227 s00115 |
(2)分步执行
- 第一步 执行命令
1 |
:%s/{"address".*"ticketThemeExt":"//g |
- 第二步 执行命令
1 |
:%s/",".*//g |
3.举例2
如下内容
1 2 3 |
1, 2, 3 |
需要把换行符去掉,变成
1 |
1,2,3。 |
此时命令如下,其中’\n’是换行符
1 |
:%s/\n//g |
(完)