- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Stackoverflow 上的其他地方的评论中发现了这个 sed 程序,用于打印文件的最后 5 行以外的所有行,我已经分解并逐行评论:
删除-last-5.sed
# create a label "a"
:a
# - on the last line, delete the pattern space and skip any remaining commands
$d
# - increment the current line number
# - append the current line (which was previously the next line)
# to the pattern space
N
# - on lines 2-5, branch to label "a"
2,5ba
# - print the first line of the pattern space
P
# - delete the first line of the pattern space
# - skip any remaining commands
# - if any text remains in the pattern space, when the next line is read,
# append to (rather than overwrite) the pattern space
D
一个例子是:
$ echo a b c d e f g h i j | xargs -n1 | sed -f delete-last-5.sed
a
b
c
d
e
但是,我实际上无法弄清楚它是如何工作的!据我所知,程序执行是这样的:
- _: line_number = 1
- _: pattern_space = "a"
- N: pattern_space += "\nb", line_number = 2
- 2,5ba: branch to label "a"
- N: pattern_space += "\nc", line_number = 3
- 2,5ba: branch to label "a"
- N: pattern_space += "\nd", line_number = 4
- 2,5ba: branch to label "a"
- N: pattern_space += "\ne", line_number = 5
- 2,5ba: branch to label "a"
- N: pattern_space += "\nf", line_number = 6
- P: print "a"
- D: pattern_space -= "a\n"
- _: pattern space is not empty after D, so mark pattern space to be appended to rather than overwritten
- _: line_number = 7
- _: pattern_space += "\ng"
- P: print "b"
- D: pattern_space -= "b\n"
- _: pattern space is not empty after D, so mark pattern space to be appended to rather than overwritten
- _: line_number = 8
- _: pattern_space += "\nh"
- P: print "c"
- D: pattern_space -= "c\n"
- _: pattern space is not empty after D, so mark pattern space to be appended to rather than overwritten
- _: line_number = 9
- _: pattern_space += "\ni"
- P: print "d"
- D: pattern_space -= "d\n"
- _: pattern space is not empty after D, so mark pattern space to be appended to rather than overwritten
- _: line_number = 10
- _: pattern_space += "\nj"
- $d: delete pattern_space
如果你收集所有这些打印语句,你最终会得到:
a
b
c
d
缺少第 5 行。
如何打印最后一行?我对这个程序的解释哪里出错了?
最佳答案
Sed 调试可能有点痛苦。有有限的内置选项可以帮助您:l
命令打印带有不可见字符的当前模式空间,例如打印换行符、制表符和行尾,以及 =
(GNU 扩展)打印当前行号。
将这些与您的示例输入一起使用,在 N
之前和之后打印行号和当前模式空间命令给了我们这个:
$ sed ':a;$d;=;l;N;=;l;2,5ba;P;D' <<< "$(printf 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n')"
1
a$
2
a\nb$
2
a\nb$
3
a\nb\nc$
3
a\nb\nc$
4
a\nb\nc\nd$
4
a\nb\nc\nd$
5
a\nb\nc\nd\ne$
5
a\nb\nc\nd\ne$
6
a\nb\nc\nd\ne\nf$
a
6
b\nc\nd\ne\nf$
7
b\nc\nd\ne\nf\ng$
b
...
<snip>
...
d
9
e\nf\ng\nh\ni$
10
e\nf\ng\nh\ni\nj$
e
查看 <snip>
之后的部分, 我们刚刚打印了 d
, 和 D
命令没有读取新行就把我们送回了循环的开始。我们正在查看第 9 行,其中包含 i
(已经在我们的模式空间的末尾)。
现在我们通过命令:
$d
– 忽略,我们正在查看第 9 行,这不是最后一行(参见调试输出)N
– 附加第 10 行;调试输出显示 j
已附加,我们实际上正在查看第 10 行2,5ba
– 忽略,我们已经过了第 5 行P
– 打印第一行:这是 e
的位置来自D
– 删除第一行,重新开始循环,不读下一行下一个周期:
$d
– 现在我们正在看最后一行。删除模式空间,不打印任何内容,我们完成了!我认为您分析中的错误是您在新周期开始时增加了行号,但唯一增加行号的地方是 N
。命令。 (顺便说一句,你忘了在最后几行中提到这一点。)
如果您想更深入地研究 sed 脚本,可以使用 sed 调试器:sedsed ,用 Python 编写。命令最后两行的输出如下所示:
PATT:e\nf\ng\nh\ni$
HOLD:$
COMM::a
COMM:$ d
PATT:e\nf\ng\nh\ni$
HOLD:$
COMM:N
PATT:e\nf\ng\nh\ni\nj$
HOLD:$
COMM:2,5 b a
COMM:P
e
PATT:e\nf\ng\nh\ni\nj$
HOLD:$
COMM:D
PATT:f\ng\nh\ni\nj$
HOLD:$
COMM::a
COMM:$ d
它显示每个命令的保持和模式空间。
该示例(10 行而不是 5 行)是在 Peteris Krumins' site 上解释的 100 个 sed 单行代码之一。 (示例 75),但解释不是很详细。相同的原理可以在 Eric Pement 的 sed one-liners 中找到。 (这是“单行解释”的灵感来源)。
关于algorithm - 这个 sed 程序是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41080428/
我在一本书(Interview Question)中读到这个问题,想在这里详细讨论这个问题。请点亮它。 问题如下:- 隐私和匿名化 马萨诸塞州集团保险委员会早在 1990 年代中期就有一个绝妙的主意
我最近接受了一次面试,面试官给了我一些伪代码并提出了相关问题。不幸的是,由于准备不足,我无法回答他的问题。由于时间关系,我无法向他请教该问题的解决方案。如果有人可以指导我并帮助我理解问题,以便我可以改
这是我的代码 public int getDist(Node root, int value) { if (root == null && value !=0) return
就效率而言,Strassen 算法应该停止递归并应用乘法的最佳交叉点是多少? 我知道这与具体的实现和硬件密切相关,但对于一般情况应该有某种指南或某人的一些实验结果。 在网上搜索了一下,问了一些他们认为
我想学习一些关于分布式算法的知识,所以我正在寻找任何书籍推荐。我对理论书籍更感兴趣,因为实现只是个人喜好问题(我可能会使用 erlang(或 c#))。但另一方面,我不想对算法进行原始的数学分析。只是
我想知道你们中有多少人实现了计算机科学的“ classical algorithms ”,例如 Dijkstra's algorithm或现实世界中的数据结构(例如二叉搜索树),而不是学术项目? 当有
我正在解决旧编程竞赛中的一些示例问题。在这个问题中,我们得到了我们有多少调酒师以及他们知道哪些食谱的信息。制作每杯鸡尾酒需要 1 分钟,我们需要使用所有调酒师计算是否可以在 5 分钟内完成订单。 解决
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我开始学习 Nodejs,但我被困在中间的某个地方。我从 npm 安装了一个新库,它是 express -jwt ,它在运行后显示某种错误。附上代码和错误日志,请帮助我! const jwt = re
我有一个证书,其中签名算法显示“sha256rsa”,但指纹算法显示“sha1”。我的证书 SHA1/SHA2 的标识是什么? 谢谢! 最佳答案 TL;TR:签名和指纹是完全不同的东西。对于证书的强度
我目前在我的大学学习数据结构类(class),并且在之前的类(class)中做过一些算法分析,但这是我在之前的类(class)中遇到的最困难的部分。我们现在将在我的数据结构类(class)中学习算法分
有一个由 N 个 1x1 方格组成的区域,并且该区域的所有部分都是相连的(没有任何方格无法到达的方格)。 下面是一些面积的例子。 我想在这个区域中选择一些方块,并且两个相邻的方块不能一起选择(对角接触
我有一些多边形形状的点列表,我想将其包含在我页面上的 Google map 中。 我已经从原始数据中删除了尽可能多的不必要的多边形,现在我剩下大约 12 个,但它们非常详细以至于导致了问题。现在我的文
我目前正在实现 Marching Squares用于计算等高线曲线,我对此处提到的位移位的使用有疑问 Compose the 4 bits at the corners of the cell to
我正在尝试针对给定算法的约束满足问题实现此递归回溯函数: function BACKTRACKING-SEARCH(csp) returns solution/failure return R
是否有包含反函数的库? 作为项目的一部分,我目前正在研究测向算法。我正在使用巴特利特相关性。在 Bartlett 相关性中,我需要将已经是 3 次矩阵乘法(包括 Hermitian 转置)的分子除以作
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
问题的链接是UVA - 1394 : And There Was One . 朴素的算法是扫描整个数组并在每次迭代中标记第 k 个元素并在最后停止:这需要 O(n^2) 时间。 我搜索了一种替代算法并
COM 中创建 GUID 的函数 (CoCreateGUID) 使用“分散唯一性算法”,但我的问题是,它是什么? 谁能解释一下? 最佳答案 一种生成 ID 的方法,该 ID 具有一定的唯一性保证,而不
在做一个项目时我遇到了这个问题,我将在这个问题的实际领域之外重新措辞(我想我可以谈论烟花的口径和形状,但这会使理解更加复杂).我正在寻找一种(可能是近似的)算法来解决它。 我有 n 个不同大小的容器,
我是一名优秀的程序员,十分优秀!