-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_trimmer.sh
More file actions
44 lines (35 loc) · 1.19 KB
/
csv_trimmer.sh
File metadata and controls
44 lines (35 loc) · 1.19 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
#!/bin/bash
directory="data"
# Check if directory exists
if [ ! -d "$directory" ]; then
echo "Error: Directory '$directory' not found"
exit 1
fi
# # First pass: Count and display line numbers for all CSV files
# echo "Current line counts for CSV files:"
# echo "--------------------------------"
# find "$directory" -type f -name "*.csv" | while read -r file; do
# line_count=$(wc -l < "$file")
# printf "%-70s %8d lines\n" "$file" "$line_count"
# done
# Second pass: Trim the files
echo -e "\nTrimming files..."
find "$directory" -type f -name "*.csv" | while read -r file; do
# echo "Processing: $file"
line_count=$(wc -l < "$file")
printf "%8d lines %-70s \n" "$line_count" "$file"
# Create a temporary file
temp_file="${file}.temp"
# Get the header and first 25000 data lines (total 25001 lines)
head -n 25001 "$file" > "$temp_file"
# Check if operation was successful
if [ $? -eq 0 ]; then
# Replace original file with trimmed version
mv "$temp_file" "$file"
echo "Successfully trimmed: $file"
else
rm -f "$temp_file"
echo "Error processing: $file"
fi
done
echo "All CSV files have been processed"