【发布时间】:2015-12-19 03:31:29
【问题描述】:
我将三星 A411 中的所有手机联系人保存在一个大文件中,以便导入到三星 Galaxy S3 手机/联系人中。
只有一个字段我无法使用 Kate 进行标准的“替换”。这是我需要做的:
N:米老鼠
替换为..
N:鼠标;米奇;;;
【问题讨论】:
标签: regex search replace contacts samsung-mobile
我将三星 A411 中的所有手机联系人保存在一个大文件中,以便导入到三星 Galaxy S3 手机/联系人中。
只有一个字段我无法使用 Kate 进行标准的“替换”。这是我需要做的:
N:米老鼠
替换为..
N:鼠标;米奇;;;
【问题讨论】:
标签: regex search replace contacts samsung-mobile
使用 sed:
sed -r 's/N:(\w*) (\w*)/N:\2;\1;;;/g' file.txt
所用s command的解释:
s/
N: # literal string "N:" ┐
( # begin of capture group │
\w # any word character │
* # ...repeated 0 or more times ├ regex
) # end of capture group │
# literal " " (space) │
(\w*) # same capture group as above ┘
/
N: # literal string "N:" ┐
\2 # backreference to 2nd group │
; # literal ";" ├ replacement
\1 # backreference to 1st group │
;;; # literal ";;;" ┘
/
g # apply to all matches ] flags
为了便于阅读,我使用了sed -r(扩展正则表达式)以避免在命令中转义每个括号。
【讨论】:
“N”记录现在很好,没有“N”重复。然而, “FN”行已被替换。 “FN”不应修改为 全部。
要仅替换以N: 开头的行,请插入^(匹配行首),即。 e.
sed -r 's/^N:…
【讨论】: