Files
luash/demos/data_processing.luash
2025-08-29 00:18:21 +01:00

159 lines
4.6 KiB
Plaintext

#!/usr/bin/env luash
-- Data Processing Demo: File processing, text manipulation, and analysis
print("=== Data Processing with Luash ===\n")
-- Create sample data for demonstration
print("1. Creating Sample Data:")
sample_data = [[timestamp,user,action,value
2024-01-01 10:00:00,alice,login,1
2024-01-01 10:05:00,bob,purchase,25.50
2024-01-01 10:10:00,alice,logout,1
2024-01-01 10:15:00,charlie,login,1
2024-01-01 10:20:00,bob,purchase,15.75
2024-01-01 10:25:00,charlie,purchase,45.00]]
-- Write sample data to file
local file = io.open("sample_data.csv", "w")
file:write(sample_data)
file:close()
print(" Sample CSV data created: sample_data.csv")
-- File analysis using shell commands
print("\n2. File Analysis:")
line_count = `wc -l < sample_data.csv`
print(" Total lines: " .. line_count)
file_size = `du -h sample_data.csv | cut -f1`
print(" File size: " .. file_size)
-- Data extraction and filtering
print("\n3. Data Extraction:")
print(" Purchase transactions:")
!grep "purchase" sample_data.csv
-- Using Lua for more complex processing
print("\n4. Advanced Processing with Lua:")
local data_file = io.open("sample_data.csv", "r")
local purchases = {}
local users = {}
-- Skip header line
data_file:read("*line")
-- Process each line
for line in data_file:lines() do
local timestamp, user, action, value = line:match("([^,]+),([^,]+),([^,]+),([^,]+)")
if action == "purchase" then
table.insert(purchases, {
timestamp = timestamp,
user = user,
amount = tonumber(value)
})
end
users[user] = true
end
data_file:close()
-- Calculate statistics
local total_amount = 0
local purchase_count = #purchases
for i = 1, purchase_count do
total_amount = total_amount + purchases[i].amount
end
local user_count = 0
for user, _ in pairs(users) do
user_count = user_count + 1
end
print(" Statistics:")
print(" - Total users: " .. user_count)
print(" - Total purchases: " .. purchase_count)
print(" - Total revenue: $" .. string.format("%.2f", total_amount))
print(" - Average purchase: $" .. string.format("%.2f", total_amount / purchase_count))
-- Log file processing
print("\n5. Log Processing:")
log_content = `tail -20 /var/log/system.log 2>/dev/null || echo "Log not accessible"`
if log_content ~= "Log not accessible" then
print(" Recent system log entries found")
else
-- Create a sample log for demonstration
sample_log = [[2024-01-01 10:00:01 INFO: Application started
2024-01-01 10:00:05 DEBUG: Database connection established
2024-01-01 10:00:10 WARN: High memory usage detected
2024-01-01 10:00:15 ERROR: Failed to connect to external API
2024-01-01 10:00:20 INFO: Retrying API connection
2024-01-01 10:00:25 INFO: API connection restored]]
local log_file = io.open("sample.log", "w")
log_file:write(sample_log)
log_file:close()
print(" Created sample.log for demonstration")
print(" Log analysis:")
-- Count log levels
error_count = `grep -c "ERROR" sample.log`
warn_count = `grep -c "WARN" sample.log`
info_count = `grep -c "INFO" sample.log`
print(" - Errors: " .. error_count)
print(" - Warnings: " .. warn_count)
print(" - Info messages: " .. info_count)
end
-- Text transformation
print("\n6. Text Transformation:")
input_text = "Hello World, this is a TEST message"
-- Using shell commands for text processing
upper_text = `echo "` .. input_text .. `" | tr '[:lower:]' '[:upper:]'`
print(" Uppercase: " .. upper_text)
word_count = `echo "` .. input_text .. `" | wc -w`
print(" Word count: " .. word_count)
-- Using Lua for more complex transformations
words = {}
for word in input_text:gmatch("%w+") do
table.insert(words, word)
end
print(" Words: " .. table.concat(words, ", "))
print(" Reversed: " .. table.concat(words, " "):reverse())
-- Batch file processing
print("\n7. Batch Processing:")
-- Create multiple sample files
for i = 1, 3 do
local batch_file = io.open("batch_" .. i .. ".txt", "w")
batch_file:write("Sample content for file " .. i .. "\n")
batch_file:write("Line 2 of file " .. i .. "\n")
batch_file:close()
end
print(" Created 3 batch files")
-- Process all batch files
batch_files = `ls batch_*.txt`
total_lines = 0
for filename in batch_files:gmatch("[^\n]+") do
local lines = `wc -l < ` .. filename
total_lines = total_lines + tonumber(lines)
print(" " .. filename .. ": " .. lines .. " lines")
end
print(" Total lines across all files: " .. total_lines)
-- Cleanup
print("\n8. Cleanup:")
!rm -f sample_data.csv sample.log batch_*.txt
print(" Temporary files cleaned up")
print("\n✓ Data processing demo completed!")