一個awk函數, 模仿rev
以前用 bash 寫了一個, 如下
# bash function , usage: reverse string , ex: reverse ABCDE , --------> EDCBA
reverse() {
local i char
local output=""
local word=$1
local wlength=${#word}
for (( i=$(( wlength - 1 )) ; i>=0 ; i-- ))
do
char=${word:$i:1}
output="${output}${char}"
done
echo "$output"
return 0
}
#--------End of function----------------------------------
今天放假,用awk**屏艘粋? ;) , 歡迎改良,意見
#-------BEGIN of function----------------------------------------------
# function reverse, awk version , usage: reverse(STRING)
function reverse(STRING, out, x, inlength) {
inlength=length(STRING)
while ( inlength > 0 ) {
x=substr(STRING, inlength, 1)
out=out x
inlength--
}
return out
}
#----------END of function---------------------------------------------------
試用的畫面
fang@bash ~
$ cat revword.awk
#! /bin/gawk -f
function reverse(STRING, out, x, inlength) {
inlength=length(STRING)
while ( inlength > 0 ) {
x=substr(STRING, inlength, 1)
out=out x
inlength--
}
return out
}
END{
print reverse($1), reverse($2), reverse($3), reverse($4)
}
fang@bash ~
$ echo This is my sentence. | ./revword.awk
sihT si ym .ecnetnes
fang@bash ~
$