sir*_*von 6 bash perl awk grep sed
我查看了有关搜索和替换的其他答案,但我无法理解这些模式.
如何更改文件的这一部分(行号153 ... 156)
let view = string.utf8
offset.pointee += string.substring(to: range.lowerBound).utf8.count
length.pointee = Int32(view.distance(from:range.lowerBound.samePosition(in: view), to:range.upperBound.samePosition(in: view)))
return token
Run Code Online (Sandbox Code Playgroud)
并用下面的行替换它?
let view:String.UTF8View = string.utf8
if let from = range.lowerBound.samePosition(in: view),
let to = range.upperBound.samePosition(in: view) {
offset.pointee += Int32(string[string.startIndex..<range.lowerBound].utf8.count)
length.pointee = Int32(view.distance(from: from, to: to))
return token
} else {
return nil
}
Run Code Online (Sandbox Code Playgroud)
这可能对你有用(GNU sed & Bash):
sed $'153r replacementFile\n;153,156d' file
Run Code Online (Sandbox Code Playgroud)
或对于大多数 sed:
sed -e '153r replacementFile' -e '153,156d' file
Run Code Online (Sandbox Code Playgroud)
或者,如果您更喜欢:
sed '153,156c\ let view:String.UTF8View = string.utf8\
if let from = range.lowerBound.samePosition(in: view),\
let to = range.upperBound.samePosition(in: view) {\
offset.pointee += Int32(string[string.startIndex..<range.lowerBound].utf8.count)\
length.pointee = Int32(view.distance(from: from, to: to))\
return token\
} else {\
return nil\
}' file
Run Code Online (Sandbox Code Playgroud)
注意第一个\保留前导空格,除最后一行之外的每一行都需要附加\. 当空行由单个行表示时,SO 中的降价格式不正确\(因此我删除了替换的第二行),但大多数 shell 应该如此。