w3resource

Bash Text Sorting and Filtering: Exercises & Solutions

1.

Sort lines in alphabetical order:

Write a Bash script that sorts the lines of a text file named "temp.txt" in alphabetical order and saves the result in a new file named "sorted_data.txt".

Code:

#!/bin/bash

# Sort the lines of "temp.txt" in alphabetical order and save the result to "sorted_data.txt"
sort temp.txt > sorted_data.txt

# Check if sorting was successful
if [ $? -eq 0 ]; then
    echo "Lines sorted successfully. Result saved to sorted_data.txt."
else
    echo "Failed to sort lines."
fi

Output:

ad@DESKTOP-3KE0KU4:~$ cat temp.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Lines sorted successfully. Result saved to sorted_data.txt.
ad@DESKTOP-3KE0KU4:~$ cat sorted_data.txt

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

Explanation:

In the exercise above,

  • sort temp.txt > sorted_data.txt: This command sorts the lines of the file "temp.txt" in alphabetical order and redirects the output to a new file named "sorted_data.txt".
  • if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message.

2.

Sort lines in reverse alphabetical order:

Write a Bash script that sorts the lines of a text file named "temp.txt" in reverse alphabetical order and saves the result in a new file named "reverse_sorted_data.txt".

Code:

#!/bin/bash

# Sort the lines of "temp.txt" in reverse alphabetical order and save the result to "reverse_sorted_data.txt"
sort -r temp.txt > reverse_sorted_data.txt

# Check if sorting was successful
if [ $? -eq 0 ]; then
    echo "Lines sorted in reverse order successfully. Result saved to reverse_sorted_data.txt."
else
    echo "Failed to sort lines in reverse order."
fi

Output:

ad@DESKTOP-3KE0KU4:~$ cat temp.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Lines sorted in reverse order successfully. Result saved to reverse_sorted_data.txt.
ad@DESKTOP-3KE0KU4:~$ cat reverse_sorted_data.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.
This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.

Explanation:

In the exercise above,

  • sort -r temp.txt > reverse_sorted_data.txt: This command sorts the lines of the file "temp.txt" in reverse alphabetical order and redirects the output to a new file named "reverse_sorted_data.txt".
  • if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message.

3.

Sort lines by line length:

Write a Bash script that sorts the lines of a text file named "temp.txt" by their length (number of characters) and saves the result in a new file named "length_sorted_data.txt".

Code:

#!/bin/bash

# Sort the lines of "temp.txt" by their length and save the result to "length_sorted_data.txt"
sort -n -k2 temp.txt > length_sorted_data.txt

# Check if sorting was successful
if [ $? -eq 0 ]; then
    echo "Lines sorted by length successfully. Result saved to length_sorted_data.txt."
else
    echo "Failed to sort lines by length."
fi 

Output:

ad@DESKTOP-3KE0KU4:~$ cat temp.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Lines sorted by length successfully. Result saved to length_sorted_data.txt.
ad@DESKTOP-3KE0KU4:~$ cat length_sorted_data.txt

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

Explanation:

In the exercise above,

  • sort -n -k2 temp.txt > length_sorted_data.txt: This command sorts the lines of the file "temp.txt" by their length using the -n flag for numerical sorting and the -k2 flag to specify sorting based on the second field (which is the length of each line). The sorted output is then redirected to a new file named "length_sorted_data.txt".
  • if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message.

4.

Sort Lines Numerically:

Write a Bash script that sorts the lines of a text file named "temp.txt" numerically and saves the result in a new file named "sorted_numbers.txt".

Code:

#!/bin/bash

# Sort the lines of "temp.txt" numerically and save the result to "sorted_numbers.txt"
sort -n temp.txt > sorted_numbers.txt

# Check if sorting was successful
if [ $? -eq 0 ]; then
    echo "Lines sorted numerically successfully. Result saved to sorted_numbers.txt."
else
    echo "Failed to sort lines numerically."
fi

Output:

ad@DESKTOP-3KE0KU4:~$ cat temp.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Lines sorted numerically successfully. Result saved to sorted_numbers.txt.
ad@DESKTOP-3KE0KU4:~$ cat sorted_numbers.txt

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

Explanation:

In the exercise above,

  • sort -n temp.txt > sorted_numbers.txt: This command sorts the lines of the file "temp.txt" numerically using the -n flag and redirects the sorted output to a new file named "sorted_numbers.txt".
  • if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message.

5.

Filter Lines Containing a Specific Word:

Write a Bash script that filters the lines of a text file named "temp.txt" to include only those containing a specific word (e.g., "keyword") and saves the result in a new file named "filtered_data.txt".

Code:

#!/bin/bash

# Filter lines containing the word "last" from "temp.txt" and save to "filtered_data.txt"
grep "last" temp.txt > filtered_data.txt

# Check if filtering was successful
if [ $? -eq 0 ]; then
    echo "Lines containing 'keyword' filtered successfully. Result saved to filtered_data.txt."
else
    echo "Failed to filter lines containing 'keyword'."
fi

Output:

ad@DESKTOP-3KE0KU4:~$ cat temp.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Lines containing 'keyword' filtered successfully. Result saved to filtered_data.txt.
ad@DESKTOP-3KE0KU4:~$ cat filtered_data.txt
This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.

Explanation:

In the exercise above,

  • grep "keyword" temp.txt > filtered_data.txt: This command filters lines containing the word "keyword" from the file "temp.txt" and redirects the filtered output to a new file named "filtered_data.txt".
  • if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message.

6.

Filter Lines Not Containing a Specific Word:

Write a Bash script that filters the lines of a text file named "temp.txt" to exclude those containing a specific word (e.g., "exclude_word") and saves the result in a new file named "filtered_data.txt".

Code:

#!/bin/bash

# Filter lines excluding the word "exclude_word" from "temp.txt" and save to "filtered_data.txt"
grep -v "Bash" temp.txt > filtered_data.txt

# Check if filtering was successful
if [ $? -eq 0 ]; then
    echo "Lines excluding 'Bash' filtered successfully. Result saved to filtered_data.txt."
else
    echo "Failed to filter lines excluding 'Bash'."
fi 

Output:

ad@DESKTOP-3KE0KU4:~$ cat temp.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
ad@DESKTOP-3KE0KU4:~$ ./test1.sh
./test1.sh: line 1: nes: command not found
Lines excluding 'Bash' filtered successfully. Result saved to filtered_data.txt.
ad@DESKTOP-3KE0KU4:~$ cat filtered_data.txt

ad@DESKTOP-3KE0KU4:~$

Explanation:

In the exercise above,

  • grep -v "exclude_word" temp.txt > filtered_data.txt: This command filters lines excluding the word "exclude_word" from the file "temp.txt" and redirects the filtered output to a new file named "filtered_data.txt".
  • if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message.

7.

Filter Lines by Line Length:

Write a Bash script that filters the lines of a text file named "temp.txt" based on their length (e.g., lines with more than 50 characters) and saves the result in a new file named "filtered_data.txt".

Code:

#!/bin/bash

# Filter lines with more than 120 characters from "temp.txt" and save to "filtered_data.txt"
awk 'length > 120' temp.txt > filtered_data.txt

# Check if filtering was successful
if [ $? -eq 0 ]; then
    echo "Lines with more than 120 characters filtered successfully. Result saved to filtered_data.txt."
else
    echo "Failed to filter lines with more than 50 characters."
fi

Output:

ad@DESKTOP-3KE0KU4:~$ cat file1.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.

This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Lines with more than 120 characters filtered successfully. Result saved to filtered_data.txt.
ad@DESKTOP-3KE0KU4:~$ cat filtered_data.txt
This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/. 

Explanation:

In the exercise above,

  • awk 'length > 120' temp.txt > filtered_data.txt: This command uses awk to filter lines from "temp.txt" where the length is greater than 120 characters and redirects the filtered output to a new file named "filtered_data.txt".
  • if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message.

8.

Filter Lines by Line Prefix:

Write a Bash script that filters the lines of a text file named "temp.txt" to include only those starting with a specific prefix (e.g., "prefix_") and saves the result in a new file named "filtered_data.txt".

Code:

#!/bin/bash

# Filter lines starting with "prefix_" from "temp.txt" and save to "filtered_data.txt"
grep '^prefix_' temp.txt > filtered_data.txt

# Check if filtering was successful
if [ $? -eq 0 ]; then
    echo "Lines starting with 'prefix_' filtered successfully. Result saved to filtered_data.txt."
else
    echo "Failed to filter lines starting with 'prefix_'."
fi  

Output:

rg@DESKTOP-3KE0KU4:~$ cat temp.txt
prefix_hello
prefix_world
prefix_example
not_prefixed_line
prefix_text
rg@DESKTOP-3KE0KU4:~$ ./test1.sh
Lines starting with 'prefix_' filtered successfully. Result saved to filtered_data.txt.
rg@DESKTOP-3KE0KU4:~$ cat filtered_data.txt
prefix_hello
prefix_world
prefix_example
prefix_text

Explanation:

In the exercise above,

  • grep '^prefix_' temp.txt > filtered_data.txt: This command uses "grep" to filter lines from "temp.txt" that start with the specified prefix ("prefix_") and redirects the filtered output to a new file named "filtered_data.txt".
  • if [ $? -eq 0 ]; then ... else ... fi: This construct checks the exit status of the previous command. If the exit status is 0 (indicating success), it prints a success message; otherwise, it prints a failure message.

9.

Remove Duplicate Lines:

Write a Bash script that removes duplicate lines from a text file named "data.txt" and saves the result in a new file named "unique_data.txt".

Code:

#!/bin/bash

# Remove duplicate lines from "temp.txt" and save to "unique_data.txt"
sort -u temp.txt > unique_data.txt

# Check if removal was successful
if [ $? -eq 0 ]; then
    echo "Duplicate lines removed successfully. Result saved to unique_data.txt."
else
    echo "Failed to remove duplicate lines."
fi

Output:

ad@DESKTOP-3KE0KU4:~$ cat temp.txt
This is line 1.
This is line 2.
This is line 3.
This is line 1.
This is line 4.
This is line 2.
This is line 5.

ad@DESKTOP-3KE0KU4:~$ ./test1.sh
Duplicate lines removed successfully. Result saved to unique_data.txt.
ad@DESKTOP-3KE0KU4:~$ cat unique_data.txt

This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.

Explanation:

In the exercise above,

  • sort -u temp.txt: This command sorts the contents of the file "temp.txt" and removes duplicate lines. The -u option stands for "unique". So, it sorts the lines and ensures only unique lines are output.
  • > unique_data.txt: Redirects the output of the "sort" command to a new file named "unique_data.txt".
  • if [ $? -eq 0 ]; then: This checks if the exit status of the previous command (sort) is equal to 0, which indicates success.
  • echo "Duplicate lines removed successfully. Result saved to unique_data.txt.": If the exit status is 0, it prints a success message indicating that duplicate lines were removed and the result was saved to "unique_data.txt".
  • else: If the exit status is not 0 (indicating failure), it prints a failure message.

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.