Calculate the string length in sed

Deadly 提交于 2021-02-20 02:36:29

问题


I was forced to calculate the string length in sed. The string is always a nonempty sequence of a's.

sed -n ':c /a/! be; s/^a/1/; s/0a/1/; s/1a/2/; s/2a/3/; s/3a/4/; s/4a/5/; s/5a/6/; s/6a/7/; s/7a/8/; s/8a/9/; s/9a/a0/; /a/ bc; :e p'

It's quite long :) So now I wonder if it is possible to rewrite this script more concisely using the y or other sed command?

I know that it is better to use awk or another tool. However, this is not a question here.

Note that the sed script basically simulates decadic full adder.


回答1:


I guess it's cheating but:

sed 's/.//;s/./\n/g'|sed -n '$='

You can certainly shorten your existing version to:

sed -n ':c s/^a/1/; s/0a/1/; s/1a/2/; s/2a/3/; s/3a/4/; s/4a/5/; s/5a/6/; s/6a/7/; s/7a/8/; s/8a/9/; s/9a/a0/; tc; p'

Turns out using y/// is possible but I think it only shaves off a few characters, and \u is not portable:

sed -n '
:c;
s/^a/c/;
s/\([b-j]\)a/\u\1/;
y/BCDEFGHIJ/cdefghijk/;
s/ka/ab/;
tc;
y/bcdefghijk/0123456789/;
p
'


来源:https://stackoverflow.com/questions/54481505/calculate-the-string-length-in-sed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!