Created
January 4, 2025 14:47
-
-
Save samesense/fc4f4bce2239e5054faaa830ee8ce9ec to your computer and use it in GitHub Desktop.
Rename tsv file columns with shorter names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create a temporary directory for storing intermediate files | |
TMPDIR=${TMP:-/tmp} | |
HEADER_FILE="$TMPDIR/header_$(date +%s%N).txt" | |
NEW_HEAD_FILE="$TMPDIR/renamed0_$(date +%s%N).txt" | |
RANDOM_FILE="$TMPDIR/renamed_$(date +%s%N).txt" | |
INPUT_REST_FILE="$TMPDIR/input_rest_$(date +%s%N).txt" | |
# Function to process input | |
process_input() { | |
local input="$1" | |
# Extract the header line | |
local header | |
if [[ -n "$input" ]]; then | |
header=$(head -n 1 "$input") | |
tail -n +2 "$input" > "$INPUT_REST_FILE" | |
else | |
read -r header | |
cat > "$INPUT_REST_FILE" | |
fi | |
# Save header to a random temporary file | |
echo -e "$header" > "$HEADER_FILE" | |
# Use llm to generate new column names | |
cat "$HEADER_FILE" | llm -m gemini-1.5-flash-latest -s \ | |
"these are column names. Make them shorter by removing unnecessary text. Only report the new column names. report one new column name per line" > "$NEW_HEAD_FILE" | |
# Read the new column names | |
if [[ ! -s "$NEW_HEAD_FILE" ]]; then | |
echo "Error: No new column names generated." >&2 | |
exit 1 | |
fi | |
local new_header | |
# Remove commas, trim spaces, and ensure the header is tab-delimited | |
new_header=$(tr ',' '\t' < "$NEW_HEAD_FILE" | sed 's/^ //;s/ \t/\t/g' | tr '\n' '\t' | sed 's/\t$//') | |
# Print the new header | |
echo -e "$new_header" | |
# Print the rest of the file or input | |
cat "$INPUT_REST_FILE" | |
# Clean up | |
rm -f "$HEADER_FILE" "$INPUT_REST_FILE" "$RANDOM_FILE" | |
} | |
# Main logic to handle piped input or file | |
if [[ -t 0 ]]; then | |
# No piped input; check for file argument | |
if [[ -n "$1" && -f "$1" ]]; then | |
process_input "$1" | |
else | |
echo "Usage: $0 <file> or pipe input to the script" >&2 | |
exit 1 | |
fi | |
else | |
# Handle piped input | |
process_input | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment