p='[:punct:]' s='[:space:]'
sed -Ee'1!{/\n/!b' -e\} \
-e's/(\n*)(.*)/ \2 \1/' \
-e"s/is[$p]?[$s]/\n&/g" \
-e"s/([^$s])\n/\1/g;1G" \
-e:c -e"s/\ni(.* )\n{3}/u\1/" \
-e"/\n$/!s/\n//g;/\ni/G" \
-e's//i/;//tc' \
-e's/^ (.*) /\1/;P;$d;N;D'
那一点sed
恰好承载着is
从一行到下一行的出现次数。它应该可靠地处理is
每行的es,并且它不需要同时缓冲旧行-它只为is
遇到的每一个字符保留一个换行符,这不是另一个单词的一部分。
结果是它将仅修改文件中的第三次出现-并且每行将进行计数。因此,如果文件看起来像:
1. is is isis
2. is does
...它将打印...
1. is is isis
2. us does
它首先通过在每行的开头和结尾处插入一个空格来处理边缘情况。这使单词边界更容易确定。
接下来,它is
通过\n
在所有出现的数字前is
紧跟零个或一个标点字符后跟一个空格的位置插入前划线,来寻找有效的es 。它会进行另一遍处理,并删除所有\n
紧跟在前面的非空格字符的后缀。留下的标记将匹配is.
,is
但不匹配this
或?is
。
接下来,它将每个标记收集到字符串的尾部-对于\ni
一行中的每个匹配项,它将\n
在字符串的尾部附加一条ewline,并将其替换为i
or或u
。如果\n
在字符串的尾部连续收集了3 条线,则使用u-否则使用i。第一次使用au也是最后一次-替换启动了一个无限循环,最终归结为此get line, print line, get line, print line,
类推。
在每个try循环周期结束时,它将清除插入的空格,仅打印到图案空间中第一个出现的换行符,然后再次执行。
我将在l
循环的开头添加一个ook命令,如下所示:
l; s/\ni(.* )\n{9}/u\1/...
...并查看此输入的工作原理:
hai this is linux.
hai this is unix.
hai this is mac.
hai this is unchanged is.
...所以这是它的作用:
hai this \nis linux. \n$ #behind the scenes
hai this is linux. #actually printed
hai this \nis unix. \n\n$ #it builds the marker string
hai this is unix.
\n\n\n$ #only for lines matching the
\n\n\n$ #pattern - and not otherwise.
hai this \nis mac. \n\n\n$ #here's the match - 3 ises so far in file.
hai this us mac. #printed
hai this is unchanged is. #no look here - this line is never evaled
is
每行更多es 可能更有意义:
nthword()( p='[:punct:]' s='[:space:]'
sed -e '1!{/\n/!b' -e\} \
-e 's/\(\n*\)\(.*\)/ \2 \1/' \
-e "s/$1[$p]\{0,1\}[$s]/\n&/g" \
-e "s/\([^$s]\)\n/\1/g;1G;:c" \
-e "${dbg+l;}s/\n$1\(.* \)\n\{$3\}/$2\1/" \
-e '/\n$/!s/\n//g;/\n'"$1/G" \
-e "s//$1/;//tc" -e 's/^ \(.*\) /\1/' \
-e 'P;$d;N;D'
)
几乎是同一回事,但是写成带有POSIX BRE和基本参数处理。
printf 'is is. is? this is%.0s\n' {1..4} | nthword is us 12
...得到...
is is. is? this is
is is. is? this is
is is. is? this us
is is. is? this is
...并且如果我启用${dbg}
:
printf 'is is. is? this is%.0s\n' {1..4} |
dbg=1 nthword is us 12
...我们可以反复观看...
\nis \nis. \nis? this \nis \n$
is \nis. \nis? this \nis \n\n$
is is. \nis? this \nis \n\n\n$
is is. is? this \nis \n\n\n\n$
is is. is? this is
\nis \nis. \nis? this \nis \n\n\n\n\n$
is \nis. \nis? this \nis \n\n\n\n\n\n$
is is. \nis? this \nis \n\n\n\n\n\n\n$
is is. is? this \nis \n\n\n\n\n\n\n\n$
is is. is? this is
\nis \nis. \nis? this \nis \n\n\n\n\n\n\n\n\n$
is \nis. \nis? this \nis \n\n\n\n\n\n\n\n\n\n$
is is. \nis? this \nis \n\n\n\n\n\n\n\n\n\n\n$
is is. is? this \nis \n\n\n\n\n\n\n\n\n\n\n\n$
is is. is? this us
is is. is? this is