我该如何编写这个 sed 脚本?

我该如何编写这个 sed 脚本?

读取输入数字“n”,并将 0 1 ... n 写入输出“n”次,每个数字之间用空格分隔。例如:n=3 1 2 3 1 2 3 1 2 3

非常感谢!

答案1

我确信这可以改进,但我有一个sed针对一位数的解决方案:

h; # put count in holding space
s/9/8 9/ # replace 9 with 8 9
s/8/7 8/ # and so on ...
s/7/6 7/ # ensuring that the pattern space
s/6/5 6/ # ends up as 1 ... n
s/5/4 5/
s/4/3 4/
s/3/2 3/
s/2/1 2/
p # print pattern space
x # exchange pattern and hold space
/9/{s/9/8/;x;p;x} # replace 9 with 8
/8/{s/8/7/;x;p;x} # and so on ...
/7/{s/7/6/;x;p;x} # effectively counting down
/6/{s/6/5/;x;p;x} # exchanging, printing and exchanging
/5/{s/5/4/;x;p;x} # each time
/4/{s/4/3/;x;p;x} # so that the pattern space
/3/{s/3/2/;x;p;x} # is printed n times
/2/{s/2/1/;x;p;x}
/1/d # we have reached one, we're done

相关内容