forked from anandapteversent/well_architected_framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_processor.sh
More file actions
28 lines (22 loc) · 792 Bytes
/
file_processor.sh
File metadata and controls
28 lines (22 loc) · 792 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
27
28
#!/bin/bash
# Check if correct number of arguments is passed
if [ "$#" -ne 3 ]; then
echo "Usage: $0 input_file output_file column_name"
exit 1
fi
input_file="$1"
output_file="$2"
column_name="$3"
# Get the column number of the column to remove
column_number=$(head -n 1 "$input_file" | tr ',' '\n' | nl -v 0 | grep -w "$column_name" | cut -f 1)
# Check if the column exists
if [ -z "$column_number" ]; then
echo "Column '$column_name' not found in the input file."
exit 1
fi
# Use awk to remove the column and create a new CSV file
awk -v col="$column_number" -F, '{
$col = ""; sub(",,", ",", $0); sub(",$", "", $0); sub("^,", "", $0);
print
}' "$input_file" > "$output_file"
echo "Column '$column_name' has been removed and the output saved to '$output_file'."