我需要用5个字符连续字符反转所有字符串。例如:
hello hi adams sde
abcde abs
必需的输出:
olleh hi smada sde
edcba abs
我用过:
sed -n 's\(a-z]\)\([a-z]\)\([a-z]\)\([a-z]\)\([a-z]\)/\5\4\3\2\1/p'
它会反转除“adams”之外的所需字符串。请帮我解决这个问题。
答案 0 :(得分:1)
看起来并不是“adams”没有被替换,而是你的命令只替换了第一个匹配的实例。试试这个:
sed -n 's/\([a-z]\)\([a-z]\)\([a-z]\)\([a-z]\)\([a-z]\)/\5\4\3\2\1/pg'
从手册:
The s command can be followed by zero or more of the following flags:
g Apply the replacement to all matches to the regexp, not just the first.
(snip)
答案 1 :(得分:1)
使用awk
awk '{
for(i=1;i<=NF;i++) {
if(length($i)==5) {
v=""
for(o=length($i);o>0;o--) {
v=v substr($i,o,1)
}
$i=v
}
}
}1' file
输出
$ more file
hello hi adams sde
abcde abs
$ ./shell.sh
olleh hi smada sde
edcba abs