sed: Reverse chars of lines
1
1 7.6 Reverse Characters of Lines
1 ===============================
1
1 This script can be used to reverse the position of characters in lines.
1 The technique moves two characters at a time, hence it is faster than
1 more intuitive implementations.
1
1 Note the 'tx' command before the definition of the label. This is
1 often needed to reset the flag that is tested by the 't' command.
1
1 Imaginative readers will find uses for this script. An example is
1 reversing the output of 'banner'.(1)
1
1 #!/usr/bin/sed -f
1
1 /../! b
1
1 # Reverse a line. Begin embedding the line between two newlines
1 s/^.*$/\
1 &\
1 /
1
1 # Move first character at the end. The regexp matches until
1 # there are zero or one characters between the markers
1 tx
1 :x
1 s/\(\n.\)\(.*\)\(.\n\)/\3\2\1/
1 tx
1
1 # Remove the newline markers
1 s/\n//g
1
1 ---------- Footnotes ----------
1
1 (1) This requires another script to pad the output of banner; for
1 example
1
1 #! /bin/sh
1
1 banner -w $1 $2 $3 $4 |
1 sed -e :a -e '/^.\{0,'$1'\}$/ { s/$/ /; ba; }' |
1 ~/sedscripts/reverseline.sed
1