-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbye-lines.sh
More file actions
executable file
·130 lines (112 loc) · 3.42 KB
/
Copy pathbye-lines.sh
File metadata and controls
executable file
·130 lines (112 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
#==============================================================================
# title : bye-lines.sh
# description : This script provides various text processing options.
# author : Timothy Merritt
# date : 2024-11-02
# version : 0.2
# usage : bash bye-lines.sh [file]
# bash_version : 5.2
#==============================================================================
LOGO="
_ _ _
| |__ _ _ ___ | (_)_ __ ___ ___
| '_ \| | | |/ _ \ | | | '_ \ / _ \/ __|
| |_) | |_| | __/ | | | | | | __/\__ \\
|_.__/ \__, |\___| |_|_|_| |_|\___||___/
|___/
v.2
Let's clean this up, shall we?
"
#==============================================================================
# Core Functionality
#==============================================================================
# Display help message
help_message() {
echo "$LOGO"
echo "Usage: bye-lines.sh [file]"
echo "Provides various text processing options for a given file."
echo
echo "Options:"
echo " -h, --help Display this help message."
echo
echo "Examples:"
echo " bye-lines.sh file.txt"
}
# Check for a valid file input
if [[ -z "$1" ]] || [[ "$1" == "-h" || "$1" == "--help" ]]; then
help_message
exit 0
elif [[ ! -f "$1" ]]; then
echo "Error: File '$1' does not exist."
exit 1
fi
# Create a temporary file to store results
temp_file=$(mktemp)
#==============================================================================
# Text Processing Functions
#==============================================================================
# Add empty lines to a file
add_empty_lines() {
sed 'G' "$1" >"$temp_file"
}
# Remove empty lines from a file
remove_empty_lines() {
sed '/^[[:space:]]*$/d' "$1" >"$temp_file"
}
# Remove leading whitespace from each line
remove_leading_whitespace() {
sed 's/^[[:space:]]*//' "$1" >"$temp_file"
}
# Remove trailing whitespace from each line
remove_trailing_whitespace() {
sed 's/[[:space:]]*$//' "$1" >"$temp_file"
}
# Convert tabs to spaces
convert_tabs_to_spaces() {
sed 's/\t/ /g' "$1" >"$temp_file"
}
# Convert spaces to tabs
convert_spaces_to_tabs() {
sed 's/ /\t/g' "$1" >"$temp_file"
}
#==============================================================================
# User Interaction
#==============================================================================
# Display the logo and prompt for options
echo "$LOGO"
echo "File: $1"
echo "Choose an option:"
options=("Remove empty lines" "Add empty lines" "Remove leading whitespace"
"Remove trailing whitespace" "Convert tabs to spaces" "Convert spaces to tabs" "Exit")
select _ in "${options[@]}"; do
case $REPLY in
1) remove_empty_lines "$1" ;;
2) add_empty_lines "$1" ;;
3) remove_leading_whitespace "$1" ;;
4) remove_trailing_whitespace "$1" ;;
5) convert_tabs_to_spaces "$1" ;;
6) convert_spaces_to_tabs "$1" ;;
7)
echo "Exiting. No changes made."
exit 0
;;
*)
echo "Invalid option. Please try again."
continue
;;
esac
break
done
# Show the changes before confirmation
echo "Changes preview:"
diff -u "$1" "$temp_file" || echo "No changes detected."
# Confirm overwrite
read -p "Would you like to overwrite the original file with these changes? (y/n): " answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
mv "$temp_file" "$1"
echo "Changes applied to '$1'"
else
echo "No changes were applied to '$1'"
rm "$temp_file"
fi