
Security News
CISA’s 2025 SBOM Guidance Adds Hashes, Licenses, Tool Metadata, and Context
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Readlines is a powerful and versatile Ruby library for file manipulation and processing. It provides a wide range of functionality to read, analyze, and modify the contents of text files efficiently.
:start
, :body
, or :end
).To use the Readlines library in your Ruby project, install it via RubyGems:
gem install readlines
Or add it to your Gemfile:
gem 'readlines'
Run bundle install
to install the library and its dependencies.
Start by requiring readlines
in your Ruby script:
require 'readlines/read'
Create an instance of the Readlines::ReadDuc
class with the filename (or full path):
file_name = 'file.txt'
read = Readlines::ReadDuc.new(file_name)
# Search for the word "example" in the file
read = Readlines::ReadDuc.new('data.txt')
results = read.search_about('example', show_lines: true)
# Display the number of occurrences
puts "Number of occurrences: #{results.count}"
# Read the entire file content
content = read.read_lines
puts content
# Count characters in the file or a specific line
total_characters = read.character_count
puts total_characters
line_characters = read.character_count(line_specific: 3)
puts line_characters
# Count words in the file or a specific line
total_words = read.word_count
puts total_words
line_words = read.word_count(line_specific: 3)
puts line_words
# Replace specific patterns in the file
updated_content = read.replace(/example/, 'new_example')
puts updated_content
# Replace multiple patterns at once
pattern_replacement = {/example/ => 'new_example', /test/ => 'new_test'}
updated_content = read.replace_multiple_patterns(pattern_replacement)
puts updated_content
# Replace characters in a CSV column
updated_csv = read.replace_csv_value(2, 'old_value', 'new_value')
puts updated_csv
# Replace special characters with a specified replacement string
updated_content = read.replace_special_characters('_')
puts updated_content
# Delete empty lines from the file
updated_content = read.delete_empty_lines
puts updated_content
# Delete specific lines with the option to remove empty lines
updated_content = read.delete_lines([2, 4, 6], delete_space: true)
puts updated_content
# Delete unwanted characters
updated_content = read.delete_unwanted_characters('!@#$%^')
puts updated_content
# Delete duplicate lines
unique_content = read.delete_duplicate_lines
puts unique_content
# Delete specific columns in a CSV file
updated_csv = read.delete_csv_columns([1, 3])
puts updated_csv
# Split the file into 3 parts
read.split_file(3)
# Split by size (in bytes)
read.split_file_by_size(1024) # 1 KB
# Split by delimiter
read.split_by_delimiter('---')
# Split by a pattern
read.split_by_pattern(/Chapter \d+/)
# Merge multiple files
merged_file = read.merge_files(['/path/to/file1.txt', '/path/to/file2.txt'])
puts merged_file
# Merge files with a separator
merged_file_with_separator = read.merge_files(['/file1.txt', '/file2.txt'], separator: '---')
puts merged_file_with_separator
# Convert encoding
read.convert_encoding('UTF-8', 'ISO-8859-1')
# Convert file format to CSV
converted_csv = read.convert_to_format('csv')
puts converted_csv
The Readlines library provides custom error classes for handling specific exceptions:
These error classes help in handling and debugging exceptions that may occur while using the library.
Contributions are welcome! If you find bugs, have suggestions, or want to add features, please open an issue or submit a pull request on the GitHub repository.
When contributing, please ensure that your code follows the existing coding style and includes appropriate tests.
The Readlines library is open-source and released under the MIT License.
FAQs
Unknown package
We found that readlines demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.