һ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**һ? ;) , gӭҊ
#-------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 ~
$ Ҍc
function reverse(STRING, out, x, inlength) {
#һ STRING ⲿoą, public, out, x inlength ǺȵĔ
#awk ] local, my , private ָֻ֮Îׂո_ͳɾֲ
inlength=length(STRING) # õL
while ( inlength > 0 ) { # Oloopingl
x=substr(STRING, inlength, 1) # substr õַһĸ..֮
out=out x # xķض׃, awk ÿոӷBӵ, out=out ϴεԪ
inlength-- # ÿһfp 1
}
return out #YloopingѽY׃
}
ϣc, ҲǸ֣ҽ xx ݹһ:
[quote][font=Courier][color=black][/color][color=blue]-(dearvoid@LinuxEden:Forum)-(~/void/awk)-
[$$=3887 $?=0][/color] [color=red]; cat rev.awk
[/color][color=black]#!/bin/awk -f
function rev(str)
{
if (length(str) <= 1) {
return str
} else {
return rev(substr(str, 2)) substr(str, 1, 1)
}
}
BEGIN {
for (ii = 1; ii < ARGC; ii++) {
printf "%s\n", rev(ARGV[color=black][[/color]ii]);
}
}
[/color][color=blue]-(dearvoid@LinuxEden:Forum)-(~/void/awk)-
[$$=3887 $?=0][/color] [color=red]; ./rev.awk This is my sentence.
[/color][color=black]sihT
si
ym
.ecnetnes
[/color][color=blue]-(dearvoid@LinuxEden:Forum)-(~/void/awk)-
[$$=3887 $?=0][/color] [color=red]; bye
[/color][color=black][/color][/font][/quote]
[[i] dearvoid 2008-3-22 14:29 ༭ [/i]] ஔe^ַLȲň,䌍Щscript ã؏ʹ :lol
[[i] twf_cc 2008-3-22 14:51 ༭ [/i]] ұȽPerlreverse
˰ַתǰʷת
[code]
[root@andLinux ~]# perl -le ' $str = reverse "@ARGV"; print $str ' abc def
fed cba
[root@andLinux ~]# perl -le ' @str = reverse @ARGV; print "@str" ' abc def
def abc
[/code] Ҳ awk reverse , ûҵ, perl ǿ :) ϲgawk Сɾ@, ׃ $ % @ :lol
[code]
shellݹ麯
rever (){ if(( ${#1} >0 ));then rever(${1:1});echo -n ${1:0:1};fi }
awk汾
awk 'BEGIN{FS="";ORS=""}{for(i=NF;i>0;i--){print i};printf "\n"}'
[/code] ֣ҵĿijɹ :lol
ܺõ awk FS ORS ,bash echo -n , !!
ҳ:
[1]