我有文件
try.txt
RAM 142 149 131
Cache 456 152 184
我想为该行的每个值计算maximum, minimum, median
预期输出:
Min= 131 Max=149 Median=142
Min=152 Max=456 Median=184
这是我尝试过的。
for itr in {1..2}
do
awk "FNR == $itr { c=0;size=NF;
for(i=2;i<=size;i++)
arr[c++] =$i;
for(i=0;i<c;i++)
{
for (j=i+1;j<c;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
print "Min=" arr[0] "Max=" arr[2] "Median=" arr[1]
}" try.txt
done
为了处理输出,我创建了一个数组来保存每行的$ 2,$ 3,$ 4,但是不幸的是,它没有使用。创建数组以计算中位数的主要目的,因为元素必须按照排序顺序计算中位数。请帮助我为每行的值创建一个数组,以计算最小值,最大值,中位数。
答案 0 :(得分:2)
对于仅包含3个数字的有限输入,我们可以:
cat <<EOF >file
LoginActivity 142 149 131
StorageCheckActivity 456 152 184
EOF
# remove the leading word
<file cut -d' ' -f2- |
# for each 3 arguments, print them on separate line, sort them, remove newlines
xargs -n3 sh -c 'printf "%s\n" "$@" | sort | tr "\\n" " "' -- |
# for each of 3 arguments from the input, print them in nice formatting using printf
xargs -n3 sh -c 'printf "Min=%d Max=%d Median=%d\n" "$1" "$3" "$2"' --
将输出:
Min=131 Max=149 Median=142
Min=152 Max=456 Median=184
答案 1 :(得分:2)
有趣的是,有3个比较:
awk '{ x=$2; y=$3; z=$4; }
(x > y) {t=x;x=y;y=t}
(y > z) {t=y;y=z;z=t}
(x > y) {t=x;x=y;y=t}
{print "Min="x" Max="z" Median="y}' file
答案 2 :(得分:1)
使用快速的perl脚本(然后从shell脚本调用)进行操作:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw/say/;
while (<>) {
chomp;
my @nums = sort { $a <=> $b } (split)[1,2,3];
say "Min=$nums[0] Max=$nums[2] Median=$nums[1]";
}
示例:
$ ./example.pl try.txt
Min=131 Max=149 Median=142
Min=152 Max=456 Median=184
或者作为单线:
$ perl -lane 'printf "Min=%d Max=%d Median=%d\n", (sort { $a <=> $b } @F[1,2,3])[0,2,1]' try.txt
Min=131 Max=149 Median=142
Min=152 Max=456 Median=184
但是,正如您所问的那样,这是一个针对gawk的awk版本:
$ gawk '{
arr[1] = $2; arr[2] = $3; arr[3] = $4;
asort(arr, arr, "@val_num_asc");
printf "Min=%d Max=%d Median=%d\n", arr[1], arr[3], arr[2];
}' try.txt
答案 3 :(得分:1)
晚些时候参加聚会,但这是另一个Supplier<SessionToken>
const things = [ {
location: 'Lynnwood, Wa',
keyword: 'Video Production',
user: null,
profile: null,
id: '5c659a55783fad6667853dd4' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597e4783fad6667853d8b' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597cc783fad6667853d8a' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c7783fad6667853d89' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d88' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d87' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659288783fad6667853d86' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659219783fad6667853d84' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d83' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d82' } ];
function getDuplicates(arry) {
var duplicates = [];
arry.forEach((item, index) => {
if(arry.indexOf(item.location) != index && arry.indexOf(item.keyword) != index) {
duplicates.push(item);
}
});
return duplicates;
}
/*
Expected Result: (Excludes first occurance of dup but returns the rest)
[
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597cc783fad6667853d8a' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c7783fad6667853d89' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d88' },
{
location: 'Las Vegas',
keyword: 'Cleaners',
user: null,
profile: null,
id: '5c6597c2783fad6667853d87' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659219783fad6667853d84' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d83' },
{
location: 'Lagos, La',
keyword: 'Top Home',
user: null,
profile: null,
id: '5c659218783fad6667853d82' }
]
*/