我怎样才能保留以后对它们进行排序号码

我怎样才能保留以后对它们进行排序号码

本文介绍了我怎样才能保留以后对它们进行排序号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这听起来像这样一个问题:写一个shell脚本,用于在命令行中的每个文件将输出
是不是从键盘读取数k较长的单词数。
输出必须通过字的数量进行排序。

我如何能留住每个文件的字符数,对它们进行排序?

我想是这样的:

 #!/斌/庆典
如果[#@ -ne 1]
        然后退出1
科幻
数组[$ @] =''
呼应-n给数>中
已读k
在$ @ F;

        N =`$ F | WC -c`
        如果[$ N -gt $ k]的;
        然后
                我++
                数组[我] = $ N
        科幻
DONE
回声{阵列[@]} | -n排序


解决方案

面临的挑战是:


  • 写一个shell脚本,用于在命令行中的每个文件将输出是不是从键盘读取数 K 长的字数。输出必须通过字的数量进行排序。

我拒绝回答提示 - 命令接受参数。我会与威廉Pursell的建议,即数是第一个参数去 - 这是一个合理的解决方案。另一种使用类似 -l 23 选项的长度(和其他选项来调整其他操作)。

的溶液我看到迄今正在计数的字的数量,但不言比给定长度长的数量。这是个问题。对于这一点,我觉得 AWK 是合适的:

 的awk -v分钟= $ K'{为(i = 1; I< = NF;我++),如果(长度($ I)GT =分钟)打印$ I ; }

这产生至少在标准输出的话分钟字符每行一个。我们会做一次这样一个文件,至少在第一轮。

我们可以再算上这些话与 WC -l 的数量。最后,我们可以将数据排序数字

全部放在一起,得出:

 #!/斌/庆典案$#,在
0 | 1)回声用法:$ 0长度的文件......>&放大器; 2;出口1 ;;
ESACK = $ {1:不能提供一个空的长度}
转移在文件$ @

    回声$(AWK -v分钟= $ K'{为(i = 1; I< = NF;我++)
                                 如果(长度($ I)GT =分钟)打印$ I
                           }$文件|
            WC -l)$文件
做| -n排序

这列出与去年最句话的文件;因为最有趣的文件是在列表的末尾这是方便。如果你首先想要的高数量,添加 -r 排序

当然,如果我们使用 AWK ,我们可以改善的东西。它可以指望的长单词的数量在每个文件和打印文件的名称和数量,所以会是 AWK 的只是一个单一的调用的所有文件。这需要多一点点编程,虽然:

 #!/ bin / sh的案$#,在
0 | 1)回声用法:$ 0长度的文件......>&放大器; 2;出口1 ;;
ESACK = $ {1:不能提供一个空的长度}
转移AWK -v分钟= $ K'
    文件名!= {oldfile目录如果(oldfile目录!=){打印长字,oldfile目录}
                          oldfile目录=文件名;长字= 0
                        }
    {为(i = 1; I< = NF;我++),如果(长度($ I)GT =分钟)长字++}
    END {如果(oldfile目录!=){打印长字,oldfile目录}}
    '$ @|
-n排序

如果您有GNU AWK ,甚至还有一些方法可以内置到 AWK 的结果进行排序。

I have a problem that sounds like this: Write a shell script that for each file from the command line will output thenumber of words that are longer than the number k read from keyboard.The output must be ordered by the number of words.

How can i retain the number of characters of each file,for sorting them?

I tried something like that :

#!/bin/bash
if [ #@ -ne 1 ]
        then exit 1
fi
array[$@]=''
echo -n "Give the number>"
read k
for f in $@;
do
        n=`$f | wc -c`
        if [ $n -gt $k ];
        then
                i++
                array[i]=$n
        fi
done
echo {array[@]} | sort -n
解决方案

The challenge is:

  • Write a shell script that for each file from the command line will output the number of words that are longer than the number k read from keyboard. The output must be ordered by the number of words.

I decline to answer prompts — commands take arguments. I'll go with William Pursell's suggestion that the number is the first argument — it is a reasonable solution. An alternative uses an option like -l 23 for the length (and other options to tweak other actions).

The solutions I see so far are counting the number of words, but not the number of words longer than the given length. This is a problem. For that, I think awk is appropriate:

awk -v min=$k '{ for (i = 1; i <= NF; i++) if (length($i) >= min) print $i; }'

This generates the words at least min characters one per line on the standard output. We'll do this one file at a time, at least in the first pass.

We can then count the number of such words with wc -l. Finally, we can sort the data numerically.

Putting it all together yields:

#!/bin/bash

case "$#" in
0|1) echo "Usage: $0 length file ..." >&2; exit 1;;
esac

k=${1:?"Cannot provide an empty length"}
shift

for file in "$@"
do
    echo "$(awk -v min=$k '{ for (i = 1; i <= NF; i++)
                                 if (length($i) >= min) print $i
                           }' "$file" |
            wc -l) $file"
done | sort -n

This lists the files with the most long words last; that's convenient because the most interesting files are at the end of the list. If you want the high numbers first, add -r to the sort.

Of course, if we're using awk, we can improve things. It can count the number of long words in each file, and print the file name and the number, so there'd be just a single invocation of awk for all the files. It takes a little bit more programming, though:

#!/bin/sh

case "$#" in
0|1) echo "Usage: $0 length file ..." >&2; exit 1;;
esac

k=${1:?"Cannot provide an empty length"}
shift

awk -v min=$k '
    FILENAME != oldfile { if (oldfile != "") { print longwords, oldfile }
                          oldfile = FILENAME; longwords = 0
                        }
    { for (i = 1; i <= NF; i++) if (length($i) >= min) longwords++ }
    END { if (oldfile != "") { print longwords, oldfile } }
    ' "$@" |
sort -n

If you have GNU awk, there are even ways to sort the results built into awk.

这篇关于我怎样才能保留以后对它们进行排序号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:28