A sed command to swap first and last character of each line A sed command to swap first and last character of each line unix unix

A sed command to swap first and last character of each line


sed -E 's/(.)(.+)(.)/\3\2\1/' input.txt


You need to escape the +,

sed 's/^\(.\)\(.\+\)\(.\)$/\3\2\1/' input.txt


If you like to try some other, here is a gnu awk version

awk '{a=$1;$1=$NF;$NF=a}1' FS= OFS= input.txt

This sets a to the first character, then sets first to last and last to a
It needs gnu awk, since settings FS to nothing is not in standard awk