Sed字符串反向

sud*_*mar 0 linux sed

我正在尝试编写sed命令来反转字符串.例如:如果我输入"美国"作为输入输出应该是"acirema".

请帮忙.

仅供参考:我可以使用shell脚本,Python脚本来完成此操作.但我不能用SED做到这一点.

Tho*_*hor 6

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)

基本上这个想法是用新行标记字符串的开头和结尾,然后,当新行通过字符串移动时,交换相邻的字符.