home

Code Library

#!/usr/bin/bash
Tmp=/tmp/$$
Tmp1=/tmp/$$_$$
trap 'exit 0' INT HUP QUIT TERM ALRM USR1
trap 'rm -f "$Tmp" "$Tmp1"' EXIT
rm -f "$Tmp"  >/dev/null 2>&1
rm -f "$Tmp1"  >/dev/null 2>&1
#================================================================
# paste -d' ' file1 file2 > output_file

source ~/bash.library

# Get the filename from the command-line argument
filename="$1"

# Check if a filename is provided
if [ -z "$filename" ]; then
  echo "Usage: $0 "
  exit 1
fi

# Check if the file exists
if [ ! -f "$filename" ]; then
  echo "Error: File '$filename' not found."
  exit 1
fi

backup_file  "$filename"   ~/BACKUPS

sed 's/[[:space:]]*$//' "$filename" > $Tmp

# Find the longest line length
max_len=$(wc -L "$Tmp" | awk '{print $1}')

# Calculate the padding length
padding_length=$((max_len + 1))

# Create a temporary file to store the padded content
temp_file=$(mktemp)

# Iterate through each line of the file and pad it
while IFS= read -r line; do
  printf "%-${padding_length}s\n" "$line" >> "$temp_file" # Pad to the right
done  < "$Tmp"

# Replace the original file with the padded content
cat "$temp_file"  >   "$filename"

# echo "File '$filename' padded successfully."