Thursday, September 1, 2016

Using ruby array insert

##When I have to insert numbers that are omitted between numbers###
##For example, the file contents are as follows.

00:data
04:data
05:data
06:data
07:data
08:data
09:data
10:data
13:data
14:data
15:data
16:data
19:data

I need to insert 01,02,03 between 00 and 04. And I also need to insert 11,12.

On linux box, I may have to use awk sed or perl. But for me ruby is much easier.
The code is as follows.


[root@redhat2 ~]# cat t.rb
#!/usr/bin/env ruby

#Function to less than 9 insert method differently
def more_than_9(a,n)
    if n < 9
       a.insert(n,"0#{n}:data")
    else
       a.insert(n,"#{n}:data")
    end
end

#File open and into array
a=File.new("./test.csv").read.split("\n")
s=a.size
i=0
 while i < (s-1)
    if (a[i+1].split(":")[0].to_i - a[i].split(":")[0].to_i) > 1
     nx=a[i+1].split(":")[0].to_i

     while i+1 < nx
      n=i+1
       more_than_9(a,n)
      i=n
     end
    end
    i=i+1
 end

#Puts results
puts a

#So, the result of executing this script is as follows.
[root@redhat2 ~]# ruby t.rb
00:data
01:data
02:data
03:data
04:data
05:data
06:data
07:data
08:data
09:data
10:data
11:data
12:data
13:data
14:data
15:data
16:data
19:data

# #################################3

Finding same file size in a current directory(ruby)-dirty script

#!/usr/bin/env ruby
#This is dirty ruby script but function well though.
#More with " https://github.com/ohyoungjooung2/ruby_scripts/blob/master/same_file.rb"
#^^; Have fun.

files=`find . -type f | xargs stat -c "%s %n"`
s=files.split("\n")
#p s

sk=[]
sv=[]

i=0
s_size=s.size
while i < s_size
  sk<<s[i].split(" ")[1]
  sv<<s[i].split(" ")[0]
  i+=1
end
# files name as a  array sk
#p sk
#puts ""
# files size as a array sv
#p sv


#Creating hash
j=0
h={}
#puts "sv size is #{sv.size}"
#puts "s_size is #{s_size}"
while j < sv.size
  #puts j
  h[sk[j]]=sv[j]
  j+=1
end

#Putting h hash
#puts h

z=0

#Getting uniq values of each hash value
u=h.values.uniq
while z < h.size
# puts z
 same_files=h.select { |k,v| v==u[z] }
 if same_files.size >= 2
   fs=0
   while fs < same_files.size
    puts  "same_file size with #{u[z]} bytes are #{same_files.keys[fs]}"
    fs+=1
   end
 end
 z+=1
end



young@ubuntu-16:~/test$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
young@ubuntu-16:~/test$ chmod 700 same_file.rb
young@ubuntu-16:~/test$ ./same_file.rb
same_file size with 8941 bytes are ./1
same_file size with 8941 bytes are ./2
same_file size with 548 bytes are ./apparmor.d/abstractions/ibus
same_file size with 548 bytes are ./f.sh
same_file size with 1748 bytes are ./tttt/iiii
same_file size with 1748 bytes are ./tttt/inputrc
same_file size with 1748 bytes are ./inputrc
same_file size with 1654 bytes are ./tttt/pass
same_file size with 1654 bytes are ./tttt/passwd
same_file size with 1654 bytes are ./pass
same_file size with 1654 bytes are ./passwd
same_file size with 1654 bytes are ./ssap
same_file size with 26 bytes are ./tttt/iss
same_file size with 26 bytes are ./tttt/issue2
same_file size with 26 bytes are ./issue
same_file size with 364 bytes are ./tttt/rrrr
same_file size with 364 bytes are ./rc.local
same_file size with 738 bytes are ./same_file.rb
same_file size with 738 bytes are ./groff/man.local