GNU sed手册中有一个例子:
rev.sed
/../! b
# Reverse a line. Begin embedding the line between two newlines
s/^.*$/\
&\
/
# Move first character at the end. The regexp matches until
# there are zero or one characters between the markers
tx
:x
s/\(\n.\)\(.*\)\(.\n\)/\3\2\1/
tx
# Remove the newline markers
s/\n//g
Run Code Online (Sandbox Code Playgroud)
像这样运行:
echo america | sed -f rev.sed
Run Code Online (Sandbox Code Playgroud)
输出:
acirema
Run Code Online (Sandbox Code Playgroud)
基本上这个想法是用新行标记字符串的开头和结尾,然后,当新行通过字符串移动时,交换相邻的字符.
| 归档时间: |
|
| 查看次数: |
1940 次 |
| 最近记录: |