在文本中行首与行尾插入字符串
示例:./add_prefix_suffix.sh your_file.txt "prefix_string" "suffix_string"
#!/bin/bash
# Add_Prefix_Suffix Script
# Description: This script takes a text file and adds user-specified prefix and suffix strings
# to each line in the file.
# Usage: ./add_prefix_suffix.sh <file_path> <prefix_string> <suffix_string>
# Check if the correct number of arguments is provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <file_path> <prefix_string> <suffix_string>"
exit 1
fi
file_path="$1" # Input file path
prefix="$2" # Prefix string to add to each line
suffix="$3" # Suffix string to add to each line
# Check if the file exists
if [ ! -f "$file_path" ]; then
echo "File does not exist or is not a regular file."
exit 1
fi
# Create a temporary file to store the modified content
tempfile="tempfile.txt"
# Use a while loop to read each line from the input file
while IFS= read -r line; do
modified_line="${prefix}${line}${suffix}"
echo "$modified_line" >> "$tempfile"
done < "$file_path"
# Replace the original file with the modified content
mv "$tempfile" "$file_path"
echo "Processing complete, the file has been updated."