How to Match Words in a Shell Script

A shell script is a small program written using one of the Linux or Unix shells such as bash, csh, or sh. The script is written in plain ASCII text and can be opened with any text editor. Since the script is a plain text file, you can use the Unix command "grep" to search for matching words within the script. Using the "-n" option with the "grep" command will provide you with the number of the line that contains the matching word. All Unix text editors have the ability to show line numbers next to the text.

Things You'll Need

  • Linux or Unix operating system
Show More

Instructions

    • 1

      Open a terminal window. This is where you will type the following commands.

    • 2

      Type "cd script_directory" to navigate into the directory where your shell script is held.

    • 3

      Type the command "grep -n word example.sh" to find the word within the shell script. Replace "word" with the word or string you are searching for and "example.sh" with the name of your script. The output will provide you with the line number and print the complete line that contains the matching word.

    • 4

      Open the shell script in any text editor such as vi or gedit.

    • 5

      Set the text editor preferences to show the line numbers. In vi type ":set number" to show the line numbers. In gedit click "Display line numbers" in the "Preferences" dialog found in the "Edit" menu.

    • 6

      Locate the line number from the "grep" output in the text file.

Tips & Warnings

  • You can match words in multiple shell scripts with the command "grep -n word example1.sh example2.sh" The output will show the file name, line number and complete line of text for each matching word.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured