Write a bash script to sort the lines in a text file by the number of characters
ID: 3829240 • Letter: W
Question
Write a bash script to sort the lines in a text file by the number of characters on each line.
Conditions:
The input file will contain only printing characters, spaces, tabs, and newlines.
The input file will not contain lines longer than 512 characters.
Requirements:
Read from standard input, write to standard output.
Output all of the input lines unchanged.
Output the line with the fewest characters first, the line with the next fewest characters next, ... , and the line with the most characters last.
For lines with the same number of characters, output the line with the fewest nonblank characters first, the line with the next fewest nonblank characters next, ... , and the line with the most nonblank characters last.
Include all of the following:
comments with your name, the date, and the assignment
comments with instructions for using the program
descriptive names and/or comments explaining variables & functions comments explaining any non-obvious control flow
indentation of code blocks
Hints:
work on the simplest cases first
think about the structure of your data and logic before you write any code start with the smallest possible amount of code
make sure your existing code works before adding new code
add the smallest possible amount of new code, then test again
output variable values, loop counts, etc. for debugging
test your code with a variety of inputs
Explanation / Answer
There are two solutions i can think of to solve the problem. Following are the details.
Note that, since we are reading directly from standing input. I have bolded the input part which you can see below. Once you gave input and press ctrl+d. You will see the required output generated.
1) Simple solution - one line command
Script:
#!/bin/bash
# script name: sol1.sh
# script to sort based on number of characters
# '-' stands for reading from command line in cat
# using awk we are computing length of the line and adding as first column, then after sorting on first column(size of line)
# we are removing first column so it looks like same as given file
cat -|awk '{print length($0), $0}'|sort -k1 -n|cut -d' ' -f2-
Execution run and output:
186590cb0725:Chegg bonkv$ ./sol1.sh
1
123 12
1212 1313 1313
1233 1313 13131 1
1313 1313 13131
1
123 12
1212 1313 1313
1313 1313 13131
1233 1313 13131 1
186590cb0725:Chegg bonkv$
2) Pure scripting solution in Bash
Script:
#!/bin/bash
# script name: sol2.sh
# script to sort lines based on number of characters
# declaring an array variable input for the stdin going to be read
declare -a input
# reading each line one by one from stdin
while read str
do
# check if length of string already exists
if [ -z "${input[${#str}]}" ]
then
# if no creating index for new length
input[${#str}]="$str"
else
# lines with equal length are added here
input[${#str}] = "${input[${#str}]} $str"
fi
done
# printing the final list of lines sorted by size
for line in "${!input[@]}"
do
echo "${input[$line]}"
done
Execution run and output:
186590cb0725:Chegg bonkv$ ./sol2.sh
krishna is a good
krishna is a
krishna
krishna is a
krishna
krishna is a
krishna is a
krishna is a good
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.