I want to sort by tab delimited by ruby

Note that the tab-delimited text was sorted to make it easier to see.

First, the underlying tab-delimited text

profile.txt #LR utf-8 
john	m	19
micheal	m	28
abbie	f	31
dabid	m	17
claire	f	26

From the left, "Name, Gender, Year" are tab-delimited. I would like to sort them by name.

First, check how the text itself is output.

 File.open("meibo.txt") do |text|
   text.each_line do |line|
    p line
  end
end

When you expand the file with

$ ruby example.rb
"john\tm\t19\n"
"micheal\tm\t28\n"
"abbie\tf\t31\n"
"dabid\tm\t17\n"
"claire\tf\t26\n"

Since it is expressed as, make an array of each line excluding tab space and line feed code Sort and output as standard.

Rough flow

  1. Open the file and make it readable
  2. Sort by name
  3. Standard output

1. Open the file and load it

Put each row in the array once, like [name, gender, age].

profile = []

 File.open("profile.txt") do |text|
   text.each_line do |line|
    profile << line.chomp.split("\t")
  end
end

By .chomp and tab-separated to remove \ n at the end of each line, by .split ("\ t")

$ ruby example.rb
[["john", "m", "19"],
["micheal", "m", "28"],
["abbie", "f", "31"],
["dabid", "m", "17"],
["claire", "f", "26"]]

The array profile was neatly[name, gender, age]and only the necessary data was obtained.

2. Sort by name

Since ruby has a sort_by method, use this method to sort by name.

 profile_sort =  profile.sort_by{|man| man[0]}
pp profile_sort

The output result is

$ ruby example.rb
[["abbie", "f", "31"],
["claire", "f", "26"],
["dabid", "m", "17"],
["john", "m", "19"],
["micheal", "m", "28"]]

And it was safely sorted in order of name.

3. Standard output

When you reach this point, output the sorted array, redirect it, and sort it.

 profile_sort.each do |text|
  puts text.join("\t")
end

Concatenate the strings of the array by connecting them with \ t. It will be tab-delimited when output with puts. Also, puts will output with a line break, so bother withputs text.join ("\ t") + "\ n" You don't have to write a line feed code.

Do this and the output will be

$ ruby example.rb
abbie	f	31
claire	f	26
dabid	m	17
john	m	19
micheal	m	28

Yes, you have successfully sorted the tab-delimited files.

Recommended Posts

I want to sort by tab delimited by ruby
I want to use arrow notation in Ruby
[Ruby] I want to do a method jump!
I want to delete files managed by Git
I want to get the value in Ruby
I want to convert characters ...
I want to perform high-speed prime factorization in Ruby (ABC177E)
I want to create a Parquet file even in Ruby
[Ruby] I want to reverse the order of the hash table
Swift: I want to chain arrays
I want to use FormObject well
I want to convert InputStream to String
I want to docker-compose up Next.js!
Webpack and webpacker I want to tell Ruby people right now
I want to add a browsing function with ruby on rails
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to limit the input by narrowing the range of numbers
I want to change the value of Attribute in Selenium of Ruby
[Ruby] I want to output only the odd-numbered characters in the character string
I want to develop a web application!
I want to write a nice build.gradle
I want to eliminate duplicate error messages
I want to make an ios.android app
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to write a unit test!
I want to install PHP 7.2 on Ubuntu 20.04.
[Ruby] I want to extract only the value of the hash and only the key
[Ruby] I want to display posted items in order of newest date
I want to perform Group By processing with Stream (group-by-count, group-by-sum, group-by-max)
I want to stop Java updates altogether
I tried to build Ruby 3.0.0 from source
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
Try to sort classes by enumeration type
[RxSwift] I want to deepen my understanding by following the definition of Observable
I want to test Action Cable with RSpec test
I want to output the day of the week
Run R from Java I want to run rJava
[Swift] I want to draw grid lines (squares)
I want to send an email in Java.
I want to graduate from npm install properly [2020]
I want to use java8 forEach with index
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
I want to simply write a repeating string
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
[Ruby basics] I tried to learn modules (Chapter 1)
I want to play with Firestore from Rails
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to reduce simple mistakes. To command yourself.
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
[Ruby] When you want to replace multiple characters
[Ruby] I want to make a program that displays today's day of the week!