![如何仅使用 sed 反转 word 中的小写字符](https://linux22.com/image/965016/%E5%A6%82%E4%BD%95%E4%BB%85%E4%BD%BF%E7%94%A8%20sed%20%E5%8F%8D%E8%BD%AC%20word%20%E4%B8%AD%E7%9A%84%E5%B0%8F%E5%86%99%E5%AD%97%E7%AC%A6.png)
使用 sed 反转一行中的每个单词
Description
-----------
The job to do is reversing every word of a line.
that a word is a sequence of lowercase alphabets
Raw Input
---------
112358 is a fibonacci sequence...
a test line
124816 1392781
final line...
Desired Output
--------------
112358 si a iccanobif ecneuqes...
a tset enil
124816 1392781
lanif enil...
答案1
这个 sed 脚本可以完成这个工作:
#!/usr/bin/sed
# Put a \n in front of the line and goto begin.
s/^/\n/
bbegin
# Marker for the loop.
:begin
# If after \n is a lower case sequence, copy its last char before \n and loop.
s/\n\([a-z]*\)\([a-z]\)/\2\n\1/
tbegin
# If after \n is not a lower case sequence, copy it before \n and loop.
s/\n\([^a-z]*[^a-z]\)/\1\n/
tbegin
# Here, no more chars after \n, simply remove it before printing the new line.
s/\n//