readlines 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/gem-push.yml +45 -0
- data/.gitignore +46 -0
- data/.rspec +0 -0
- data/CHANGELOG.md +36 -0
- data/CODE_OF_CONDUCT.md +0 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +30 -0
- data/LICENSE +21 -0
- data/README.md +349 -0
- data/Rakefile +9 -0
- data/examples/example1.rb +13 -0
- data/examples/example10.rb +13 -0
- data/examples/example11.rb +13 -0
- data/examples/example12.rb +13 -0
- data/examples/example13.rb +13 -0
- data/examples/example14.rb +13 -0
- data/examples/example15.rb +13 -0
- data/examples/example16.rb +13 -0
- data/examples/example17.rb +13 -0
- data/examples/example18.rb +13 -0
- data/examples/example19.rb +13 -0
- data/examples/example2.rb +13 -0
- data/examples/example20.rb +13 -0
- data/examples/example21.rb +13 -0
- data/examples/example22.rb +13 -0
- data/examples/example23.rb +13 -0
- data/examples/example24.rb +13 -0
- data/examples/example25.rb +13 -0
- data/examples/example26.rb +13 -0
- data/examples/example27.rb +13 -0
- data/examples/example28.rb +13 -0
- data/examples/example29.rb +13 -0
- data/examples/example3.rb +13 -0
- data/examples/example30.rb +13 -0
- data/examples/example31.rb +13 -0
- data/examples/example32.rb +13 -0
- data/examples/example33.rb +13 -0
- data/examples/example34.rb +13 -0
- data/examples/example35.rb +13 -0
- data/examples/example36.rb +13 -0
- data/examples/example37.rb +13 -0
- data/examples/example38.rb +13 -0
- data/examples/example4.rb +13 -0
- data/examples/example5.rb +13 -0
- data/examples/example6.rb +13 -0
- data/examples/example7.rb +13 -0
- data/examples/example8.rb +13 -0
- data/examples/example9.rb +13 -0
- data/lib/read.rb +9 -0
- data/lib/readlines/read.rb +263 -0
- data/lib/readlines/readlines/check.rb +16 -0
- data/lib/readlines/readlines/content.rb +45 -0
- data/lib/readlines/readlines/convert.rb +43 -0
- data/lib/readlines/readlines/count.rb +58 -0
- data/lib/readlines/readlines/delete.rb +89 -0
- data/lib/readlines/readlines/error.rb +13 -0
- data/lib/readlines/readlines/file.rb +20 -0
- data/lib/readlines/readlines/info.rb +48 -0
- data/lib/readlines/readlines/merge.rb +38 -0
- data/lib/readlines/readlines/pattern.rb +22 -0
- data/lib/readlines/readlines/replace.rb +50 -0
- data/lib/readlines/readlines/search.rb +62 -0
- data/lib/readlines/readlines/sort.rb +18 -0
- data/lib/readlines/readlines/split.rb +79 -0
- data/lib/readlines/readlines/version.rb +7 -0
- data/readlines.gemspec +25 -0
- metadata +182 -0
@@ -0,0 +1,263 @@
|
|
1
|
+
# readlines/lib/readlines/read.rb
|
2
|
+
require_relative 'readlines/check'
|
3
|
+
require_relative 'readlines/content'
|
4
|
+
require_relative 'readlines/convert'
|
5
|
+
require_relative 'readlines/count'
|
6
|
+
require_relative 'readlines/delete'
|
7
|
+
require_relative 'readlines/error'
|
8
|
+
require_relative 'readlines/file'
|
9
|
+
require_relative 'readlines/info'
|
10
|
+
require_relative 'readlines/merge'
|
11
|
+
require_relative 'readlines/pattern'
|
12
|
+
require_relative 'readlines/replace'
|
13
|
+
require_relative 'readlines/search'
|
14
|
+
require_relative 'readlines/sort'
|
15
|
+
require_relative 'readlines/split'
|
16
|
+
require 'fileutils'
|
17
|
+
require 'json'
|
18
|
+
|
19
|
+
module Readlines
|
20
|
+
class ReadDuc
|
21
|
+
include Readlines::Count
|
22
|
+
include Readlines::Delete
|
23
|
+
include Readlines::Convert
|
24
|
+
include Readlines::Search
|
25
|
+
include Readlines::Replace
|
26
|
+
include Readlines::File
|
27
|
+
include Readlines::Merge
|
28
|
+
include Readlines::Info
|
29
|
+
include Readlines::Pattern
|
30
|
+
include Readlines::Sort
|
31
|
+
include Readlines::Split
|
32
|
+
include Readlines::Content
|
33
|
+
attr_reader :file_path
|
34
|
+
|
35
|
+
def initialize(file_path, count: nil, split: nil)
|
36
|
+
raise Readlines::Error::MissingFilePathError, 'You should provide a file path, e.g., "/path/to/file.txt"' if file_path.nil? || file_path.empty?
|
37
|
+
|
38
|
+
@file_path = file_path
|
39
|
+
@count_keyword = count
|
40
|
+
@split_delimiter = split
|
41
|
+
end
|
42
|
+
|
43
|
+
include Readlines::Count
|
44
|
+
include Readlines::Delete
|
45
|
+
include Readlines::Convert
|
46
|
+
include Readlines::Search
|
47
|
+
include Readlines::Replace
|
48
|
+
include Readlines::File
|
49
|
+
include Readlines::Merge
|
50
|
+
include Readlines::Info
|
51
|
+
include Readlines::Pattern
|
52
|
+
include Readlines::Sort
|
53
|
+
include Readlines::Split
|
54
|
+
include Readlines::Content
|
55
|
+
|
56
|
+
# Count operations
|
57
|
+
def line_count
|
58
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
59
|
+
line_count_now()
|
60
|
+
end
|
61
|
+
|
62
|
+
def character_count(line_specific: false)
|
63
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
64
|
+
character_count_now(line_specific: line_specific)
|
65
|
+
end
|
66
|
+
|
67
|
+
def word_count(line_specific: false)
|
68
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
69
|
+
word_count_now(line_specific: line_specific)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Read operations
|
73
|
+
def read_lines
|
74
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
75
|
+
read_lines_now()
|
76
|
+
end
|
77
|
+
|
78
|
+
def info
|
79
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
80
|
+
info_now()
|
81
|
+
end
|
82
|
+
|
83
|
+
# Search operations
|
84
|
+
def search_about(value, show_lines: false)
|
85
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
86
|
+
search_about_now(value, show_lines: show_lines)
|
87
|
+
end
|
88
|
+
|
89
|
+
def search_multiple_patterns(patterns)
|
90
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
91
|
+
search_multiple_patterns_now(patterns)
|
92
|
+
end
|
93
|
+
|
94
|
+
def search_in_range(start_line, end_line, pattern)
|
95
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
96
|
+
search_in_range_now(start_line, end_line, pattern)
|
97
|
+
end
|
98
|
+
|
99
|
+
def search_logical_patterns(patterns, operator)
|
100
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
101
|
+
raise ArgumentError, "Invalid logical operator: #{operator}" unless %w[AND OR].include?(operator)
|
102
|
+
search_logical_patterns_now(patterns, operator)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Replace operations
|
106
|
+
def replace(pattern, replacement)
|
107
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
108
|
+
replace_now(pattern, replacement)
|
109
|
+
end
|
110
|
+
|
111
|
+
def replace_special_characters(replacement)
|
112
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
113
|
+
replace_special_characters_now(replacement)
|
114
|
+
end
|
115
|
+
|
116
|
+
def replace_multiple_patterns(pattern_replacement_hash)
|
117
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
118
|
+
replace_multiple_patterns_now(pattern_replacement_hash)
|
119
|
+
end
|
120
|
+
|
121
|
+
def replace_csv_value(column_index, old_value, new_value)
|
122
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
123
|
+
replace_csv_value_now(column_index, old_value, new_value)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Delete operations
|
127
|
+
def delete_empty_lines
|
128
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
129
|
+
delete_empty_lines_now()
|
130
|
+
end
|
131
|
+
|
132
|
+
def delete_unwanted_characters(characters)
|
133
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
134
|
+
delete_unwanted_characters_now(characters)
|
135
|
+
end
|
136
|
+
|
137
|
+
def delete_lines(line_numbers, delete_space: false)
|
138
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
139
|
+
delete_lines_now(line_numbers, delete_space: delete_space)
|
140
|
+
end
|
141
|
+
|
142
|
+
def delete_duplicate_lines
|
143
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
144
|
+
delete_duplicate_lines_now()
|
145
|
+
end
|
146
|
+
|
147
|
+
def delete_csv_columns(column_indices)
|
148
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
149
|
+
delete_csv_columns_now(column_indices)
|
150
|
+
end
|
151
|
+
|
152
|
+
def delete_right_left_separator(separator, delete_right: false, delete_left: false, keep_separator: false)
|
153
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
154
|
+
delete_right_left_separator_now(separator, delete_right: delete_right, delete_left: delete_left, keep_separator: keep_separator)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Sort operations
|
158
|
+
def sort_alphabetically
|
159
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
160
|
+
sort_alphabetically_now()
|
161
|
+
end
|
162
|
+
|
163
|
+
# Split operations
|
164
|
+
def split_file(num_parts)
|
165
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
166
|
+
raise ArgumentError, "Number of parts must be greater than 0" if num_parts <= 0
|
167
|
+
split_file_now(num_parts)
|
168
|
+
end
|
169
|
+
|
170
|
+
def split_file_by_size(part_size)
|
171
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
172
|
+
raise ArgumentError, "Part size must be greater than 0" if part_size <= 0
|
173
|
+
split_file_by_size_now(part_size)
|
174
|
+
end
|
175
|
+
|
176
|
+
def split_by_delimiter(delimiter)
|
177
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
178
|
+
split_by_delimiter_now(delimiter)
|
179
|
+
end
|
180
|
+
|
181
|
+
def split_by_pattern(pattern)
|
182
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
183
|
+
split_by_pattern_now(pattern)
|
184
|
+
end
|
185
|
+
|
186
|
+
# Merge operations
|
187
|
+
def merge_files(file_paths)
|
188
|
+
raise ArgumentError, "File paths array is empty" if file_paths.empty?
|
189
|
+
merge_files_now(file_paths)
|
190
|
+
end
|
191
|
+
|
192
|
+
def merge_files_with_specific_line(file_paths, separator: "\n")
|
193
|
+
raise ArgumentError, "File paths array is empty" if file_paths.empty?
|
194
|
+
merge_files_with_specific_line_now(file_paths, separator: separator)
|
195
|
+
end
|
196
|
+
|
197
|
+
# Convert operations
|
198
|
+
def convert_encoding(from_encoding, to_encoding)
|
199
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
200
|
+
convert_encoding_now(from_encoding, to_encoding)
|
201
|
+
end
|
202
|
+
|
203
|
+
def convert_to_format(format)
|
204
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
205
|
+
convert_to_format_now(format)
|
206
|
+
end
|
207
|
+
|
208
|
+
def convert_to_array
|
209
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
210
|
+
convert_to_array_now()
|
211
|
+
end
|
212
|
+
|
213
|
+
# File operations
|
214
|
+
def file_size(unit = nil)
|
215
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
216
|
+
file_size_now(unit)
|
217
|
+
end
|
218
|
+
|
219
|
+
def file_statistics
|
220
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
221
|
+
file_statistics_now()
|
222
|
+
end
|
223
|
+
|
224
|
+
# Pattern operations
|
225
|
+
def pattern_exists?(pattern)
|
226
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
227
|
+
pattern_exists_now?(pattern)
|
228
|
+
end
|
229
|
+
|
230
|
+
def extract_patterns(patterns)
|
231
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
232
|
+
extract_patterns_now(patterns)
|
233
|
+
end
|
234
|
+
|
235
|
+
# Content operations
|
236
|
+
def reverse_content
|
237
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
238
|
+
reverse_content_now()
|
239
|
+
end
|
240
|
+
|
241
|
+
def validate_content(rules)
|
242
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
243
|
+
validate_content_now(rules)
|
244
|
+
end
|
245
|
+
|
246
|
+
# Spelling operations
|
247
|
+
def check_spelling(dictionary)
|
248
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
249
|
+
check_spelling_now(dictionary)
|
250
|
+
end
|
251
|
+
|
252
|
+
# Encryption operations
|
253
|
+
def encrypt_content(key)
|
254
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
255
|
+
encrypt_content_now(key)
|
256
|
+
end
|
257
|
+
|
258
|
+
def decrypt_content(key, encrypted_file_path)
|
259
|
+
raise Readlines::Error::NotFoundError, "File not found: #{encrypted_file_path}" unless ::File.exist?(encrypted_file_path)
|
260
|
+
decrypt_content_now(key, encrypted_file_path)
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/check.rb
|
2
|
+
# Check the spelling of words in the file against a dictionary
|
3
|
+
require 'fileutils'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module Check
|
8
|
+
def check_spelling_now(dictionary)
|
9
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
10
|
+
|
11
|
+
content = ::File.read(@file_path)
|
12
|
+
misspelled_words = content.scan(/\b\w+\b/).reject { |word| dictionary.include?(word.downcase) }
|
13
|
+
misspelled_words.uniq
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/content.rb
|
2
|
+
require 'fileutils'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Readlines
|
6
|
+
module Content
|
7
|
+
def reverse_content_now
|
8
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
9
|
+
|
10
|
+
content = ::File.readlines(@file_path)
|
11
|
+
reversed_content = content.reverse.join
|
12
|
+
::File.write(@file_path, reversed_content)
|
13
|
+
reversed_content
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate_content_now(rules)
|
17
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
18
|
+
|
19
|
+
content = ::File.readlines(@file_path)
|
20
|
+
content.all? do |line|
|
21
|
+
rules.all? { |rule| line.match?(rule) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def encrypt_content_now(key)
|
26
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
27
|
+
|
28
|
+
content = ::File.read(@file_path)
|
29
|
+
encrypted_content = content.chars.map { |char| (char.ord + key).chr }.join
|
30
|
+
encrypted_file_name = "#{@file_path}.encrypted"
|
31
|
+
::File.write(encrypted_file_name, encrypted_content)
|
32
|
+
encrypted_file_name
|
33
|
+
end
|
34
|
+
|
35
|
+
def decrypt_content_now(key, encrypted_file_path)
|
36
|
+
raise Readlines::NotFoundError, "File not found: #{encrypted_file_path}" unless ::File.exist?(encrypted_file_path)
|
37
|
+
|
38
|
+
encrypted_content = ::File.read(encrypted_file_path)
|
39
|
+
decrypted_content = encrypted_content.chars.map { |char| (char.ord - key).chr }.join
|
40
|
+
decrypted_file_name = encrypted_file_path.sub('.encrypted', '')
|
41
|
+
::File.write(decrypted_file_name, decrypted_content)
|
42
|
+
decrypted_file_name
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/convert.rb
|
2
|
+
# Convert the encoding of the file
|
3
|
+
require 'fileutils'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module Convert
|
8
|
+
def convert_encoding_now(from_encoding, to_encoding)
|
9
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
10
|
+
|
11
|
+
content = ::File.read(@file_path, encoding: from_encoding)
|
12
|
+
::File.write(@file_path, content.encode(to_encoding))
|
13
|
+
end
|
14
|
+
|
15
|
+
def convert_to_format_now(format)
|
16
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
17
|
+
|
18
|
+
content = ::File.read(@file_path)
|
19
|
+
|
20
|
+
case format.downcase
|
21
|
+
when 'txt'
|
22
|
+
when 'csv'
|
23
|
+
content = content.lines.map { |line| line.strip.split(',') }.map { |fields| fields.join(',') }.join("\n")
|
24
|
+
when 'json'
|
25
|
+
data = content.lines.map { |line| line.strip.split(',') }
|
26
|
+
content = JSON.pretty_generate(data)
|
27
|
+
else
|
28
|
+
raise ArgumentError, "Unsupported format: #{format}. Supported formats are .txt, .csv, and .json."
|
29
|
+
end
|
30
|
+
|
31
|
+
converted_file_path = "#{::File.basename(@file_path, ::File.extname(@file_path))}-copy.#{format.downcase.delete('.')}"
|
32
|
+
::File.write(converted_file_path, content)
|
33
|
+
converted_file_path
|
34
|
+
end
|
35
|
+
|
36
|
+
def convert_to_array_now
|
37
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
38
|
+
|
39
|
+
content = ::File.readlines(@file_path)
|
40
|
+
content.map(&:chomp)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/count.rb
|
2
|
+
# Count the number of lines in the file based on the specified keyword and delimiter
|
3
|
+
require 'fileutils'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module Count
|
8
|
+
def line_count_now
|
9
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
10
|
+
count = 0
|
11
|
+
content = ::File.read(@file_path)
|
12
|
+
if @split_delimiter
|
13
|
+
sections = content.split(@split_delimiter)
|
14
|
+
sections.each do |section|
|
15
|
+
count += 1 if @count_keyword && section.include?(@count_keyword)
|
16
|
+
end
|
17
|
+
else
|
18
|
+
content.each_line do |line|
|
19
|
+
count += 1 if @count_keyword.nil? || line.include?(@count_keyword)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
count
|
23
|
+
end
|
24
|
+
|
25
|
+
# Read the entire content of the file
|
26
|
+
def read_lines_now
|
27
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
28
|
+
content = ::File.read(@file_path)
|
29
|
+
content
|
30
|
+
end
|
31
|
+
|
32
|
+
# Count the number of characters in the file or a specific line
|
33
|
+
def character_count_now(line_specific: false)
|
34
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
35
|
+
|
36
|
+
content = ::File.read(@file_path)
|
37
|
+
if line_specific.is_a?(Integer)
|
38
|
+
line = content.lines[line_specific - 1]
|
39
|
+
line.length
|
40
|
+
else
|
41
|
+
content.length
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Count the number of words in the file or a specific line
|
46
|
+
def word_count_now(line_specific: false)
|
47
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
48
|
+
|
49
|
+
content = ::File.read(@file_path)
|
50
|
+
if line_specific.is_a?(Integer)
|
51
|
+
line = content.lines[line_specific - 1]
|
52
|
+
line.split.length
|
53
|
+
else
|
54
|
+
content.split.length
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/delete.rb
|
2
|
+
# Delete empty lines from the file
|
3
|
+
require 'fileutils'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module Delete
|
8
|
+
def delete_empty_lines_now
|
9
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
10
|
+
content = ::File.read(@file_path)
|
11
|
+
updated_content = content.lines.reject { |line| line.strip.empty? }.join
|
12
|
+
::File.write(@file_path, updated_content)
|
13
|
+
updated_content
|
14
|
+
end
|
15
|
+
|
16
|
+
# Delete unwanted characters from the file
|
17
|
+
def delete_unwanted_characters_now(characters)
|
18
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
19
|
+
|
20
|
+
content = ::File.read(@file_path)
|
21
|
+
updated_content = content.gsub(/[#{Regexp.escape(characters)}]/, '')
|
22
|
+
::File.write(@file_path, updated_content)
|
23
|
+
updated_content
|
24
|
+
end
|
25
|
+
|
26
|
+
# Delete specific lines from the file and optionally remove empty lines
|
27
|
+
def delete_lines_now(line_numbers, delete_space: false)
|
28
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
29
|
+
|
30
|
+
content = ::File.readlines(@file_path)
|
31
|
+
updated_content = content.reject.with_index { |_, index| line_numbers.include?(index + 1) }.join
|
32
|
+
|
33
|
+
::File.write(@file_path, updated_content)
|
34
|
+
|
35
|
+
if delete_space
|
36
|
+
remove_empty_lines
|
37
|
+
end
|
38
|
+
|
39
|
+
updated_content
|
40
|
+
end
|
41
|
+
|
42
|
+
# Delete duplicate lines from the file
|
43
|
+
def delete_duplicate_lines_now
|
44
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
45
|
+
|
46
|
+
content = ::File.readlines(@file_path)
|
47
|
+
unique_content = content.uniq.join
|
48
|
+
::File.write(@file_path, unique_content)
|
49
|
+
unique_content
|
50
|
+
end
|
51
|
+
|
52
|
+
# Delete specific columns from a CSV file
|
53
|
+
def delete_csv_columns_now(column_indices)
|
54
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
55
|
+
|
56
|
+
content = ::File.readlines(@file_path)
|
57
|
+
updated_content = content.map do |line|
|
58
|
+
values = line.chomp.split(',')
|
59
|
+
column_indices.sort.reverse.each { |index| values.delete_at(index) }
|
60
|
+
values.join(',')
|
61
|
+
end.join("\n")
|
62
|
+
::File.write(@file_path, updated_content)
|
63
|
+
updated_content
|
64
|
+
end
|
65
|
+
|
66
|
+
def delete_right_left_separator_now(separator, delete_right: false, delete_left: false, keep_separator: false)
|
67
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
68
|
+
|
69
|
+
result = ::File.readlines(@file_path).map do |line|
|
70
|
+
line.strip!
|
71
|
+
next if line.empty?
|
72
|
+
|
73
|
+
parts = line.split(separator, 2)
|
74
|
+
separator_keep = keep_separator ? separator : ''
|
75
|
+
|
76
|
+
if delete_right && parts.size > 1
|
77
|
+
parts.first + separator_keep
|
78
|
+
elsif delete_left && parts.size > 1
|
79
|
+
separator_keep + parts.last
|
80
|
+
else
|
81
|
+
line
|
82
|
+
end
|
83
|
+
end.compact
|
84
|
+
|
85
|
+
::File.write(@file_path, result.join("\n"))
|
86
|
+
result
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/error.rb
|
2
|
+
|
3
|
+
module Readlines
|
4
|
+
module Error
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
class NotFoundError < Error; end
|
8
|
+
|
9
|
+
class MissingFilePathError < Error; end
|
10
|
+
|
11
|
+
class InvalidUnitError < StandardError; end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/file.rb
|
2
|
+
# Get statistics about the file
|
3
|
+
require 'fileutils'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module File
|
8
|
+
def file_statistics_now
|
9
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
10
|
+
|
11
|
+
content = ::File.read(@file_path)
|
12
|
+
{
|
13
|
+
lines: content.lines.count,
|
14
|
+
characters: content.length,
|
15
|
+
words: content.split.length
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/info.rb
|
2
|
+
# Get the file size in the specified unit
|
3
|
+
require 'fileutils'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module Info
|
8
|
+
def file_size_now(unit = nil)
|
9
|
+
size = ::File.size(@file_path)
|
10
|
+
|
11
|
+
if unit.nil?
|
12
|
+
size
|
13
|
+
else
|
14
|
+
case unit
|
15
|
+
when :gigabytes, :g
|
16
|
+
size / (1024.0 * 1024.0 * 1024.0)
|
17
|
+
when :megabytes, :m
|
18
|
+
size / (1024.0 * 1024.0)
|
19
|
+
when :kilobytes, :k
|
20
|
+
size / 1024.0
|
21
|
+
when :bytes, :b
|
22
|
+
size
|
23
|
+
else
|
24
|
+
raise Readlines::InvalidUnitError, "Invalid unit: #{unit}. Supported units are :gigabytes, :megabytes, :kilobytes, or :bytes."
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def info_now
|
30
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
31
|
+
|
32
|
+
file_info = {
|
33
|
+
created_at: ::File.birthtime(@file_path),
|
34
|
+
modified_at: ::File.mtime(@file_path),
|
35
|
+
path: ::File.absolute_path(@file_path),
|
36
|
+
size: ::File.size(@file_path),
|
37
|
+
name: ::File.basename(@file_path),
|
38
|
+
type: ::File.extname(@file_path)
|
39
|
+
}
|
40
|
+
|
41
|
+
if block_given?
|
42
|
+
yield file_info
|
43
|
+
else
|
44
|
+
file_info
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/merge.rb
|
2
|
+
# Merge multiple files into a single file
|
3
|
+
require 'fileutils'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module Merge
|
8
|
+
def merge_files_now(file_paths)
|
9
|
+
raise ArgumentError, "File paths array is empty" if file_paths.empty?
|
10
|
+
|
11
|
+
merged_content = file_paths.map do |file_path|
|
12
|
+
raise Readlines::NotFoundError, "File not found: #{file_path}" unless ::File.exist?(file_path)
|
13
|
+
::File.read(file_path)
|
14
|
+
end.join
|
15
|
+
|
16
|
+
file_name = ::File.basename(@file_path, ::File.extname(@file_path))
|
17
|
+
merged_file_name = "#{file_name}_merged#{::File.extname(@file_path)}"
|
18
|
+
::File.write(merged_file_name, merged_content)
|
19
|
+
|
20
|
+
merged_file_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def merge_files_with_specific_line_now(file_paths, separator: "\n")
|
24
|
+
raise ArgumentError, "File paths array is empty" if file_paths.empty?
|
25
|
+
|
26
|
+
merged_content = file_paths.map do |file_path|
|
27
|
+
raise Readlines::NotFoundError, "File not found: #{file_path}" unless ::File.exist?(file_path)
|
28
|
+
::File.read(file_path)
|
29
|
+
end.join(separator)
|
30
|
+
|
31
|
+
file_name = ::File.basename(@file_path, ::File.extname(@file_path))
|
32
|
+
merged_file_name = "#{file_name}_merged#{::File.extname(@file_path)}"
|
33
|
+
::File.write(merged_file_name, merged_content)
|
34
|
+
|
35
|
+
merged_file_name
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/pattern.rb
|
2
|
+
# Check if a pattern exists in the file
|
3
|
+
require 'fileutils'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module Pattern
|
8
|
+
def pattern_exists_now?(pattern)
|
9
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
10
|
+
|
11
|
+
content = ::File.read(@file_path)
|
12
|
+
content.match?(pattern)
|
13
|
+
end
|
14
|
+
|
15
|
+
def extract_patterns_now(patterns)
|
16
|
+
raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
17
|
+
|
18
|
+
content = ::File.read(@file_path)
|
19
|
+
patterns.flat_map { |pattern| content.scan(pattern) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|