uniq

Rails .each show once for duplicate results

非 Y 不嫁゛ 提交于 2019-12-03 21:53:15
Within a rails .each code, how can I eliminate duplicates from the result? PMRelationships is a relationship table that links people and movies MGRelationships is a relationship table that links genres and movies The process Im looking for is to find All a Person's genres through first PmRelationship then MgRelationship <% @pm_relationships = PmRelationship.where(:people_id => @person.id) %> <ul class="basic-info-genres"> <% @pm_relationships.each do |pm_relationship| %> <% @movie=Movie.find(pm_relationship.movie_id) %> <% @mg_relationships = MgRelationship.where(:movie_id => @movie.id) %> <%

awk命令

偶尔善良 提交于 2019-12-03 13:32:05
自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站的访问量。看看有没有黑阔搞破坏!于是收集,整理一些服务器日志分析命令,大家可以试试! 1、查看有多少个IP访问: awk '{print $1}' log_file|sort|uniq|wc -l 2、查看某一个页面被访问的次数: grep "/index.php" log_file | wc -l 3、查看每一个IP访问了多少个页面: awk '{++S[$1]} END {for (a in S) print a,S[a]}' log_file > log.txt sort -n -t ' ' -k 2 log.txt 配合sort进一步排序 4、将每个IP访问的页面数进行从小到大排序: awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -n 5、查看某一个IP访问了哪些页面: grep ^111.111.111.111 log_file| awk '{print $1,$7}' 6、去掉搜索引擎统计的页面: awk '{print $12,$1}' log_file | grep ^\"Mozilla | awk '{print $2}' |sort | uniq | wc -l 7

nginx总结

痴心易碎 提交于 2019-12-03 10:46:51
log_format⽤用于设置⽇日志格式,格式为log_format 格式名 样式 配置字段http 默认的combined log_format combined '$remote_addr - $remote_user [$time_local] ' ' "$request" $status $body_bytes_sent ' ' "$http_referer" "$http_user_agent" ‘; 不不需要配置,默认的,如果在配置会提示重复nginx: [emerg] duplicate "log_format" name "combined" in /etc/nginx/nginx.conf:45 $server_name:虚拟主机名称 $remote_addr:远程客户端的IP地址 $remote_user:远程客户端⽤用户名称,⽤用于记录浏览者进⾏行行身份验证时提供的名字,⽆无则空⽩白 [$time_local]:访问的时间与时区[12/Jun/2016:20:18:19 +0800] $request:请求的URI和HTTP协议(*) $status:记录请求返回的http状态码 200/301/302/404/403/400/502 $http_referer:来源 $http_user_agent:客户端浏览器器信息 $http_x_forwarded

MAX(), DISTINCT and group by in Cassandra

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to remodel a SQL database Cassandra such that, I can find the Cassandra equivalent for the SQL queries. I use CQL 3 and Cassandra v1.2. I modeled the db design in cassandra so that it supports the order by clauses and denormalized tables to support the join operation. However I am at sea when it comes to DISTINCT, SUM() and GROUPBY equvalents SELECT a1,MAX(b1) FROM demo1 group by a1. SELECT DISTINCT (a2) FROM demo2 where b2='sea' SELECT sum(a3), sum(b3) from demo3 where c3='water' and d3='ocean' This is like a showstopper to my

`uniq` without sorting an immense text file?

匿名 (未验证) 提交于 2019-12-03 02:26:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a stupidly large text file (i.e. 40 gigabytes as of today) that I would like to filter for unique lines without sorting the file. The file has unix line endings, and all content matches [[:print:]] . I tried the following awk script to display only unique lines: awk 'a[$0] {next} 1' stupid.txt > less_stupid.txt The thought was that I'd populate an array by referencing its elements, using the contents of the file as keys, then skip lines that were already in the array. But this fails for two reasons -- firstly, because it inexplicably

Uniq by object attribute in Ruby

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What's the most elegant way to select out objects in an array that are unique with respect to one or more attributes? These objects are stored in ActiveRecord so using AR's methods would be fine too. 回答1: Use Array#uniq with a block: @photos = @photos.uniq { |p| p.album_id } 回答2: Add the uniq_by method to Array in your project. It works by analogy with sort_by . So uniq_by is to uniq as sort_by is to sort . Usage: uniq_array = my_array.uniq_by {|obj| obj.id} The implementation: class Array def uniq_by(&blk) transforms = [] self.select do |el

How do I remove duplicate items from an array in Perl?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an array in Perl: my @my_array = ("one","two","three","two","three"); How do I remove the duplicates from the array? 回答1: You can do something like this as demonstrated in perlfaq4 : sub uniq { my %seen; grep !$seen{$_}++, @_; } my @array = qw(one two three two three); my @filtered = uniq(@array); print "@filtered\n"; Outputs: one two three If you want to use a module, try the uniq function from List::MoreUtils 回答2: The Perl documentation comes with a nice collection of FAQs. Your question is frequently asked: % perldoc -q duplicate

How do I get the unique elements from an array of hashes in Ruby?

匿名 (未验证) 提交于 2019-12-03 02:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an array of hashes, and I want the unique values out of it. Calling Array.uniq doesn't give me what I expect. a = [{:a => 1},{:a => 2}, {:a => 1}] a.uniq # => [{:a => 1}, {:a => 2}, {:a => 1}] Where I expected: [{:a => 1}, {:a => 2}] In searching around on the net, I didn't come up with a solution that I was happy with. Folks recommended redefining Hash.eql? and Hash.hash , since that is what Array.uniq is querying. Edit: Where I ran into this in the real world, the hashes were slightly more complex. They were the result of parsed

underscore/lodash unique by multiple properties

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an array of objects with duplicates and I'm trying to get a unique listing, where uniqueness is defined by a subset of the properties of the object. For example, {a:"1",b:"1",c:"2"} And I want to ignore c in the uniqueness comparison. I can do something like _.uniq(myArray,function(element) { return element.a + "_" + element+b}); I was hoping I could do _.uniq(myArray,function(element) { return {a:element.a, b:element.b} }); But that doesn't work. Is there something like that I can do, or do I need to create a comparable

Ruby: group_by operation on an array of hashes

匿名 (未验证) 提交于 2019-12-03 01:09:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have an array of hashes that represent compounds stored in boxes. database = [{ "Name" => "Compound1" , "Box" => 1 }, { "Name" => "Compound2" , "Box" => 1 }, { "Name" => "Compound2" , "Box" => 1 }, { "Name" => "Compound3" , "Box" => 1 }, { "Name" => "Compound4" , "Box" => 1 }, { "Name" => "Compound5" , "Box" => 2 }, { "Name" => "Compound6" , "Box" => 2 }, { "Name" => "Compound1" , "Box" => 3 }, { "Name" => "Compound2" , "Box" => 3 }, { "Name" => "Compound3" , "Box" => 3 }, { "Name" => "Compound7" , "Box" => 4 }] I would like to