问题
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