Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 在许多行中,这些行的值与我想要计算的值相同_Shell - Fatal编程技术网

Shell 在许多行中,这些行的值与我想要计算的值相同

Shell 在许多行中,这些行的值与我想要计算的值相同,shell,Shell,主要故障是: 785 785 788 788 883 883 883 921 921 921 921 921 921 925 925 我想计算相同的值,并将结果写入一个新文件,如下所示: 785 2 788 2 883 3 921 6 925 2 谢谢你的帮助 sort myFile.txt | uniq -c | awk '{ print $2 " " $1}' > myNewFile.txt 编辑:添加排序和删除cat以考虑注释 如果只需要至少出现4次的值: sort temp.tx

主要故障是:

785 785 788 788 883 883 883 921 921 921 921 921 921 925 925

我想计算相同的值,并将结果写入一个新文件,如下所示: 785 2 788 2 883 3 921 6 925 2

谢谢你的帮助

sort myFile.txt | uniq -c | awk '{ print $2 " " $1}' > myNewFile.txt
编辑:添加排序和删除cat以考虑注释

如果只需要至少出现4次的值:

sort temp.txt | uniq -c | sort -n | egrep -v "^ *[0-3] "  | awk '{ print $2 " " $1}'

假设您的文件名为t

您可以使用:

cat t | sort -u | while read line #read each one element in sorted and uniqye
do
   echo -n $line; # print element
   cat t | grep ${line} | wc -l # read file, get only the specified and count
done
仅打印计数>=4:


另外,如果计数>=4,我们可以将其添加到上面的命令中吗?&&谢谢。你在说什么计数?uniq要求文件已经排序。但OP没有提到这一点。另外,cat命令的作用是什么?
kent$  awk '{a[$0]++}END{for(x in a)print x, a[x]}' f
921  6
925  2
883  3
785  2
788  2
kent$  awk '{a[$0]++}END{for(x in a)if(a[x]>=4)print x, a[x]}' f    
921  6