Here’s a simple shell script that sorts the lines in a text file alphabetically. You can use the sort command to accomplish this.

Linux Shell Script to Sort Text File
Here the script is explained step by step.
!/bin/bash
#Check if a file name is provided as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 "
exit 1
fi
#Store the filename provided as an argument
filename="$1"
#Check if the file exists
if [ ! -e "$filename" ]; then
echo "File '$filename' not found."
exit 1
fi
#Check if the file is a regular file
if [ ! -f "$filename" ]; then
echo "'$filename' is not a regular file."
exit 1
fi
#Sort the file and create a sorted version
sort "$filename" > "${filename}.sorted"
#Check if the sorting was successful
if [ $? -eq 0 ]; then
echo "File '$filename' has been sorted and saved as '${filename}.sorted'."
else
echo "Sorting failed for '$filename'."
exit 1
fi
Save this script to a file, for example, sort_file.sh, and make it executable using the chmod command:
chmod +x sort_file.sh
You can then use it to sort a text file by running:
./sort_file.sh your_text_file.txt
Replace your_text_file.txt with the actual name of the file you want to sort. The sorted version of the file will be saved as your_text_file.txt.sorted. You can adjust the output file name and location as needed in the script.
Travel Neck Pillow
Trajectory Travel Neck Pillow Rest Cushion for Travel in Flight car Train Airplane with 2 Years Warranty for Sleeping for Men and Women (Grey, Polyester)
Related







You must be logged in to post a comment.