将文本中每一行指定的字符(支持存在转义字符,类似/)及这个字符之后的字符串全部删除
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <file_path> <delete_char>"
exit 1
fi
file_path="$1"
delete_char="$2"
# Check if the file exists
if [ ! -f "$file_path" ]; then
echo "File does not exist or is not a regular file."
exit 1
fi
# Escape the delete character for use in the sed command
escaped_delete_char=$(sed 's/[\/&]/\\&/g' <<< "$delete_char")
# Use a different delimiter (|) in the sed command to avoid conflicts with / in the input
# Delete the delete_char and everything after it on each line
sed -i "s|$escaped_delete_char.*$||" "$file_path"
echo "Processing complete, the original file has been updated."