我想使用 unix 命令按长度排序字符串,然后按 ASCII 顺序(大小写相等)。
string = [a-z][A-Z][0-9]
例如:
"A a b B cc ca cd" :
=> A a b B
=> ca cc cd
"Hello stackoverflow how are you today"
=> are how you
=> Hello today
=> stackoverflow
我想使用 unix 命令按长度排序字符串,然后按 ASCII 顺序(大小写相等)。
string = [a-z][A-Z][0-9]
例如:
"A a b B cc ca cd" :
=> A a b B
=> ca cc cd
"Hello stackoverflow how are you today"
=> are how you
=> Hello today
=> stackoverflow
我写了一条丑陋的(也许)awk|sort|awk
线来完成这项工作。它也可以在一个 awk 进程中完成,但是,我有点懒,只是去肮脏和快速的方式。
echo yourStr|awk '{
split($0,o); for(x in o) print length(o[x]),o[x]}'|sort -n|awk '!p{printf $2;p=$1;next}$1==p{printf " "$2}$1!=p{printf "\n"$2;p=$1}'
举个例子:
"Hello stackoverflow how are you today foo bar xoo yoo ooo"
尝试上面的行:
kent$ echo "Hello stackoverflow how are you today foo bar xoo yoo ooo"|awk '{
split($0,o); for(x in o) print length(o[x]),o[x]}'|sort -n|awk '!p{printf $2;p=$1;next}$1==p{printf " "$2}$1!=p{printf "\n"$2;p=$1}'
are bar foo how ooo xoo yoo you
Hello today
stackoverflow
用你的第一个例子测试:
kent$ echo "A a b B cc ca cd" |awk '{
pipe quote> split($0,o); for(x in o) print length(o[x]),o[x]}'|sort -n|awk '!p{printf $2;p=$1;next}$1==p{printf " "$2}$1!=p{printf "\n"$2;p=$1}'
a A b B
ca cc cd
这是使用GNU awk
. 像这样运行:
awk -f script.awk file
内容script.awk
:
BEGIN {
IGNORECASE=1
}
{
for(i=1;i<=NF;i++) {
a[length($i)][$i]++
}
}
END {
for (i in a) {
b[x++] = i + 0
}
n = asort(b)
for (j=1;j<=n;j++) {
m = asorti(a[b[j]],c)
for (k=1;k<=m;k++) {
for (l=1;l<=a[b[j]][c[k]];l++) {
r = (r ? r FS : "") c[k]
}
s = (s ? s FS : "") r
r = ""
}
print s
s = ""
}
}
使用您的输入的结果,串联:
A a B b
ca cc cd
are how you
Hello today
stackoverflow
或者,这是单线:
awk '{ for(i=1;i<=NF;i++) a[length($i)][$i]++ } END { for (i in a) b[x++] = i + 0; n = asort(b); for (j=1;j<=n;j++) { m = asorti(a[b[j]],c); for (k=1;k<=m;k++) { for (l=1;l<=a[b[j]][c[k]];l++) r = (r ? r FS : "") c[k]; s = (s ? s FS : "") r; r = "" } print s; s="" } }' IGNORECASE=1 file