2019/03/06

Hacer algo con los archivos de un directorio

path_ = "/home/www/sac3/" Dir.glob(path_ + "*").each do |f| #DoSomething Expedient.find(11975).assets.new(attachment: File.new(path_ + "#{File.basename(f)}", "r")).save end

2019/02/14

Agrupar objetos por fecha de creacion

accounts.each do |account| by_years = Tweet.where(screen_name: account).group_by {|u| u.twt_created_at_date.year } by_years.keys.sort.each do |year| by_months = by_years[year].group_by {|u| u.twt_created_at_date.month } by_months.keys.sort.each do |month| #DO SOMETHING end end end

2019/02/13

Ordenar un array con otro array

new_order = ['ARENA-PCN-PDC-DS', 'ARENA', 'DS', 'FMLN', 'GANA', 'PCN', 'PDC', 'TOTAL ARENA-PCN-PDC-DS', 'VAMOS', 'ABSTENCIONES', 'FALTANTES', 'IMPUGNADOS', 'INUTILIZADAS', 'NULOS', 'SOBRANTES']

votos.sort_by { |a| new_order.index(a[0]) }

2018/10/04

Conocer el tamaño de las tablas en una base de datos MYSQL

Cambiar "mydb" por el nombre de la base de datos:

SELECT table_name "Table Name", table_rows "Rows Count", round(((data_length + index_length)/1024/1024),2) "Table Size (MB)" FROM information_schema.TABLES WHERE table_schema = "mydb";

 fuente: https://tecadmin.net/how-to-check-mysql-database-tables-size/

2018/10/03

Utilizar sed en archivos grandes (mas de un giga)

Intente utilizar sed para eliminar varias lineas en un dump de sql, el archivo pesaba un poco menos de 2 gigas, pero no funciono luego de 15 minutos no dio resultado el siguiente comando:

 sed '/INSERT INTO `twitter_statuses`/d' > fixed_dump.sql

Luego intente comentarlo pero tampoco funciono:

sed 's/INSERT INTO `twitter_statuses`/-- INSERT INTO `twitter_statuses`/' > fixed_dump.sql

La solución fue utilizar cat y enviar la salida a sed, realizó el cambio en un momento:

cat wrong_dump.sql | sed 's/INSERT INTO `twitter_statuses`/-- INSERT INTO `twitter_statuses`/' > fixed_dump.sql