Ruby File Text Trimming Script

I want to share a ruby script for how to trim a file text above a certain line number or any condition

require 'fileutils'

class File_Text_Trimmer
    def self.trim_text(line_number)
        open('some.txt', 'r') do |f|
          open('some.txt.tmp', 'w') do |f2|
            f.each_line do |line|
               f2.write(line) unless $. < line_number
                      # this is condition    
                      #  $. < line_number    
            end
          end
        end
        FileUtils.mv 'some.txt.tmp', 'somechanged.txt'
        # to save changes in same file or to overwrite
        # FileUtils.mv 'some.txt.tmp', 'some.txt'
    end
end

In, above you can send the line_number as an argument to this method

To run on Windows 10, first install Ruby from their official site. Mostly it will be installed in C:\Ruby[version]-x[32|64].

If you have saved above script at location

D:\Ruby-Script\File_Text_Trimmer.rb

Then you can call this script like this

ruby -r "D:\Ruby-Script\File_Text_Trimmer.rb" -e "File_Text_Trimmer.trim_text 5"

One thing more some.txt file should be in same folder from where you are running above command or you can give its full absolute path.

To do, make the filename also argument base.

Thats it!

Setting time zone in AWS RDS Mysql

Recently I have to set timezone for RDS (Mysql) so that when default timestamp used for any datetime field. And from application tier if no value mentioned then it should pick the correct value according to set timezone. However, to set timezone in Mysql one could do following stuff:

  1. Set it via my.cnf
  2. @@global.time_zone
  3. @@session.time_zone

But none of above applicable to RDS. Except for last one which also not as straight forward as looked. And when you google for this then you will find that this could only be done via Db parameters group. Interestingly in default group by Mysql you do have a parameter namely default_time_zone which is also read only. So how to do that you have to create a custom parameter group. And then set the init_connect with the store procedure call, as mentioned in Fabio article.

Upon doing this stuff successfully following Fabio article via RDS interface and PHPmyadmin, I thought why not put this on web via this post to explain things a bit more. You have to do following three things.

  1. Create store procedure namely “store_time_zone” in database mysql
  2. Give permission to the user of application to this store procedure “store_time_zone”
  3. Create custom parameter group and set “init_connect” to “store_time_zone”
  4. Select the instance and apply the newly created parameter group

1. Create store procedure namely “store_time_zone” in database mysql

So let’s start implementation, first of all we need to understand that AWS RDS Mysql by default using a database name mysql to handle most of its things. So when you logged into the RDS intance via phpmyadmin, you could see on left panel.

 

mysqldb

And in this database you need to create the store procedure to set the session wise time_zone. You could do follow steps shown:

1after db selected

 

Then on the pop up, please remove the parameter as shown

3a_queryboxopen

After removing the parameter it will look like this

3querybox

 

And then put name and the body of store procedure in the pop up for new store procedure as shown

4query-pasted

Please ignore errors shown in image. The store procedure body and name is same as in Fabio article. But some minor changes for body of code.

BEGIN
IF NOT (POSITION(‘rdsadmin@’ IN CURRENT_USER()) = 1) THEN
SET SESSION time_zone = ‘Asia/Dubai’;
END IF;
END

then upon clicking go you will see success message

5success

 

 

2. Give permission to the user of application to this store procedure “store_time_zone”

This is very critical issue, so don’t forget this one. You need to give all users of your application execute right. I did that as

GRANT EXECUTE ON PROCEDURE mysql.store_time_zone TO ‘admin_shoppresta’@’%’;

 

6right

3. Create custom parameter group and set “init_connect” to “store_time_zone”

First of all login to AWS account and open RDS services from upper services menu.

rdsservice

Then you can view the RDS dashboard.

2rdsview

By default we always have the default parameter group whose parameter can not be changed that’s why even if you select the default group you will see that edit group button will remain disable. And first we need to add a new parameter group against predefined family with their version. To add parameter group click on Parameter Groups on left panel under RDS Dashboard. And then Create Parameter Group.

3rdsparametergroupview

Then on the create parameter group pop fill values as shown according to your criteria. And then click Create

 

rdspgcreATE

Now we will see the newly added parameter group as well. Now first select this group and then click on Edit parameters button.

rdspgselected

Now we could see the all parameters available for edit, so we need to scroll down and reach to init_connect parameter as all are alphabetically arranged.  And enter the command to call our created stored procedure earlier like this.

Call mysql.store_time_zone

Here we are also using dbname.nameofstoreprocedure as shown under

init_connect

Now scroll back and click the Save Changes button. Then you will again see the parameter groups. Now this newly created group still not attached to your instance.

rdspg_init_connectPmSave

4. Select the instance and apply the newly created parameter group

For this click on instances under RDS Dashboard. Then you will see all your instances running. Select the one on which you want to apply newly created parameter group. First select the instance then click on Instance Actions

rdsinstanceselect

Now click on Modify option from dropdown list

rdspg_init_connectPm

Now scroll to Database Options. And click on dropdown and then select the newly created parameter group.

rdsinstacepgselected

And then scroll down and select Apply Immediately first and then click on Continue button.

rdsinsmodifyapplyimmediate

And on next screen simply click on Modify DB Instance

rdsinsmodifycontinueclicked

And then again on instances screen. Select the instance And then click on Reboot from dropdown.

rdsintancereboot

 

And it will reboot in few seconds. And that’s it.

Now you have successfully done these changes to set the default time zone of RDS Mysql.

Note: Don’t forget to give execute permission to all users of this instance other wise application will also have error and phpmyadmin also will have same issue for users except for super user i.e. root