forked from westsurname/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrow_count.sh
More file actions
26 lines (19 loc) · 729 Bytes
/
row_count.sh
File metadata and controls
26 lines (19 loc) · 729 Bytes
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
#!/bin/bash
db="/path/to/your.db"
# Temporary file to hold table names and counts
temp_file=$(mktemp)
# Ensure temporary file gets deleted on script exit
trap "rm -f $temp_file" EXIT
# Fetch each table name from the database
table_names=$(sqlite3 "$db" "SELECT name FROM sqlite_master WHERE type='table';")
# Iterate over each table name, count its rows, and write to the temp file
for table in $table_names; do
count=$(sqlite3 "$db" "SELECT COUNT(*) FROM \"$table\";")
echo "$count|$table" >> "$temp_file"
done
# Sort the temporary file numerically by counts and then print
sort -n "$temp_file" | while IFS='|' read -r count name; do
echo "$name = $count"
done
# Clean up the temporary file
rm -f "$temp_file"